充分利用switch-case 中break的特性,更方便的求出天数。
要点: 月份倒着写,并且每个case 不要添加break;那么,程序就会一直往下运行,直到month=1,进而求出了总的天数。
以下是源代码:
package third;
import java.util.Scanner;
public class dataRequire {
static int require(int month, int day, boolean f){
int sum=0;
switch (month-1) {
case 11:
sum = sum +30;
case 10:
sum = sum +31;
case 9:
sum = sum +30;
case 8:
sum = sum +31;
case 7:
sum = sum +31;
case 6:
sum = sum +30;
case 5:
sum = sum +31;
case 4:
sum = sum +30;
case 3:
sum = sum +31;
case 2:
{if ( f)
sum = sum +28;
else
sum = sum +29;
}
case 1:
sum = sum +31;
break;
}
return sum+day ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int year, day, month;
boolean f =false;
System.out.println("请输入年月日:");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
month = sc.nextInt();
day = sc.nextInt();
if ( year %4 ==0 && year%100!=0 || year%400 ==0)
f = true;
int sum = require(month,day, f);
System.out.println("是第"+sum+"天");
}
}