java练习题目5:分支结构switch-case
题目:
从键盘分别输入年、月、日,判断这一天是当年的第几天
注:判断一年是否是闰年的标准:
1)可以被4整除,但不可被100整除
2)可以被400整除
-
import java.util.Scanner;
-
class TestSwitch5{
-
public static void main(String[] args){
-
Scanner s = new Scanner(System.in);
-
System.out.println("请输入年份:");
-
int year = s.nextInt();
-
System.out.println("请输入月份:");
-
int month = s.nextInt();
-
System.out.println("请输入日期:");
-
int day = s.nextInt();
-
int sum = 0;
-
switch(month){
-
case 12:
-
sum += 30;//11月份的总天数
-
case 11:
-
sum += 31;
-
case 10:
-
sum += 30;
-
case 9:
-
sum += 31;
-
case 8:
-
sum += 31;
-
case 7:
-
sum += 30;
-
case 6:
-
sum += 31;
-
case 5:
-
sum += 30;
-
case 4:
-
sum += 31;
-
case 3:
-
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
-
sum += 29;
-
}
-
else{
-
sum += 28;
-
}
-
case 2:
-
sum += 31;//1月份的天数
-
case 1:
-
sum += day;
-
}
-
System.out.println("您输入的是年份是"+year+"年,"+"日期是本年的第"+sum+"天");
-
}
-
}
阅读(1372) | 评论(0) | 转发(0) |