4个java小练习

package com.lddx.day1028;

import java.util.Scanner;

//判断闰年平年的练习
/*练习1:要求从控制台输入一个年份2018,判断该年是平年还是闰年
平年---365天    2月28天
闰年---366天    2月29天
闰年判断公式:
1)能被4整除,并且不能被100整除的
2)能被400整除的*/
public class Exe01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入一个年份:");
		int year=sc.nextInt();
		if((year%4==0&&year%100!=0)||year%400==0){//闰年的情况
			System.out.println(year+"是闰年");
		}else{//平年的情况
			System.out.println(year+"是平年");
		}
		

	}

}
package com.lddx.day1028;

import java.util.Scanner;

//模拟柜台收银的程序
/*
 * 练习2:模拟柜台收银的程序
要求从控制台输入商品单价,数量和付钱的金额
如果总金额满500元,打八折
如果付钱的金额够了,找零,输入“商品消费了多少,找零多少”
如果付钱的金额不够,输出金额不够,还差多少
 */
public class Exe02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入商品的单价:");
		double price=sc.nextDouble();
		System.out.println("请输入商品的数量:");
		int num=sc.hashCode();
		System.out.println("请输入付款金额:");
		double money=sc.nextDouble();
		//求总消费金额(单价*数量)
		double totalPrice=price*num;
		//判断总的消费金额如果满500,打8折
		if(totalPrice>=500){
			//totalPrice=totalPrice*0.8;
			totalPrice*=0.8;
		}
		System.out.println("商品总消费了:"+totalPrice);
		//判断付钱的金额是够了还是不够
		if(money>=totalPrice){
			System.out.println("找零"+(money-totalPrice));
		}
		else{//不够的情况
			System.out.println("还差:"+(totalPrice-money));
		}

	}

}
package com.lddx.day1028;
//练习3:从控制台输入三个整数,找出三个整数中的最大值并输出
import java.util.Scanner;

public class Exe03 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入第一个整数:");
		int a=sc.nextInt();
		System.out.println("请输入第二个整数:");
		int b=sc.nextInt();
		System.out.println("请输入第三个整数:");
		int c=sc.nextInt();
		if(a>b&&a>c){
			System.out.println("最大值:"+a);
		}else if(b>a&&b>c){
			System.out.println("最大值:"+b);
		}else{
			System.out.println("最大值:"+c);
			/*
			 * 方法2:
			 * int max=a;//假设最大值为a
			 * if(max<b){
			 * max=b;}//最大值有可能是b
			 * if(max<c){
			 * max=c;}//最大值有可能是c
			 * System.out.println("最大值为:"+max);
			 */
		}

	}

}
package com.lddx.day1028;

import java.util.Scanner;

//判断年,月下有几天练习
public class Exe04 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入年:");
		int year=sc.nextInt();
		System.out.println("请输入月:");
		int month=sc.nextInt();
		if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
			System.out.println("31天");
		else if(month==4||month==6||month==9||month==11)
			System.out.println("30天");
		else if(month==2){//闰年--29天
			if((year%4==0&&year!=100)||year%400==0)
				System.out.println("28天");
		else//平年--28天
			System.out.println("29天");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_25368751/article/details/83472754