import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入今天的年份:");
int year=scan.nextInt();
System.out.println("请输入今天的月份:");
int month=scan.nextInt();
System.out.println("请输入今天的日数:");
int day=scan.nextInt();
int days = 0;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
switch (month) {
case 1:
days=day;
case 2:
if(day<=29) {
days = 31+day;
}else
System.out.println("输入的日数有误!");
case 3:
days=31+29+day;
break;
case 4:
days=31+29+31+day;
break;
case 5:
days=31+29+31+30+day;
break;
case 6:
days=31+29+31+30+31+day;
break;
case 7:
days=31+29+31+30+31+30+day;
break;
case 8:
days=31+29+31+30+31+30+31+day;
break;
case 9:
days=31+29+31+30+31+30+31+31+day;
break;
case 10:
days=31+29+31+30+31+30+31+31+30+day;
break;
case 11:
days=31+29+31+30+31+30+31+31+30+31+day;
break;
case 12:
days=31+29+31+30+31+30+31+31+30+31+30+day;
break;
default:
System.out.println("输入的月份有误!");
}
}
else {switch (month) {
case 1:
days=day;
case 2:
if(day<=28) {
days = 31+day;
}else
System.out.println("输入的日数有误");
case 3:
days=31+28+day;
break;
case 4:
days=31+28+31+day;
break;
case 5:
days=31+28+31+30+day;
break;
case 6:
days=31+28+31+30+31+day;
break;
case 7:
days=31+28+31+30+31+30+day;
break;
case 8:
days=31+28+31+30+31+30+31+day;
break;
case 9:
days=31+28+31+30+31+30+31+31+day;
break;
case 10:
days=31+28+31+30+31+30+31+31+30+day;
break;
case 11:
days=31+28+31+30+31+30+31+31+30+31+day;
break;
case 12:
days=31+28+31+30+31+30+31+31+30+31+30+day;
break;
default:
System.out.println("输入的月份有误!");
}
}
System.out.println("今天是" + year + "年的第" + days + "天");
}
}
java入门------(if+switch语句)判断一年中的天数小程序1.0
最新推荐文章于 2022-07-18 18:52:20 发布