这样写判断有些麻烦,看看我写的,希望对你有启发:
- class CountDemo
- {
- public static void main(String[] args){
- //1-12月份天数,闰年2月28天需要特别处理
- int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31};
- int year = 2000,month = 5,sum = 0;
- for(int i=0; i<month; i++){
- if(i == 1/*2 - 1*/){
- if(year%4==0 && year%100!=0 || year%400==0){
- sum += 29;
- continue;
- }
- }
- sum += monthDays[i];
- }
- System.out.println(sum);
- //System.out.println("Hello World!");
- }
- }
复制代码 |