语句练习题4
输入某年某月某日,判断这一天是这一年的第几天?
1 Scanner sc2 = new Scanner(System.in); 2 System.out.println("请输入年份:"); 3 int year=sc2.nextInt(); 4 System.out.println("请输入月份:"); 5 int month=sc2.nextInt(); 6 System.out.println("请输入日期:"); 7 int day=sc2.nextInt(); 8 int zday=0;//定义总天数 9 boolean isRn;//判断是闰年返回真 10 if((year%4==0&&year%100!=0)||year%400==0){ 11 isRn=true; 12 }else{ 13 isRn=false; 14 } 15 for(int i=1;i<month;i++){ 16 switch(i){ 17 case 1: 18 case 3: 19 case 5: 20 case 7: 21 case 8: 22 case 10: 23 case 12: 24 zday+=31; 25 break; 26 case 2: 27 if(isRn){ 28 zday+=29; 29 }else{ 30 zday+=28; 31 } 32 break; 33 case 4: 34 case 6: 35 case 9: 36 case 11: 37 zday+=30; 38 break; 39 default: 40 System.out.println("月份输入有误,请重新输入!"); 41 } 42 } 43 zday+=day; 44 System.out.println(month+"月"+day+"日,是"+year+"年的第"+zday+"天。");
结果: