if_else语句运用
package day03;
import java.util.Scanner;
/**
* 根据输入的年和月计算天数
* @author 74599
*
*/
public class CalssDemo02 {
public static void main(String[] args) {
//创建键盘输入
Scanner key = new Scanner(System.in);
//输入年份
System.out.println("输入年份:");
int yser = key.nextInt();
//输入月份
System.out.println("输入月份:");
int month = key.nextInt();
//判断输入的月份是否有31天
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
System.out.println("31天");
//判断输入的月份是否有30天
}else if(month==4||month==6||month==9||month==11) {
System.out.println("30天");
//如果输入的是2月
}else if(month == 2){
//判断是否是闰年,如果是闰年那么就是29天,计算固定公式:年%4==0&&年%100!=0||年%400==0
if(yser%4==0&&yser%100!=0||yser%400==0) {
System.out.println("29天");
//否则平年就是28天
}else {
System.out.println("28天");
}
//胡乱输入进行提示
}else{
System.out.println("月份不正确");
}
//关闭键盘输入
key.close();
}
}
浙公网安备 33010602011771号