java 输入日期某年某月某日 查是第几天的程序问题?

import java.util.Scanner; public class Sun9 { public static void main(String[] args){ Scanner in =new Scanner(System.in); System.out.print("请输入年份:"); int year =in.nextInt(); System.out.print("请输入月份:"); int mouth = in.nextInt(); System.out.print("请输入几号:"); int day = in.nextInt(); int maxday=0; for(int i=1;i<mouth;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxday+=31; break; case 4: case 6: case 9: case 11: maxday+=30; break; case 2: maxday+=28; break; default: System.out.println("输入月份错误!"); } } if((mouth)>2&&((year%4==0)&&year%100!=0)||(year%400==0)){ maxday++; } maxday+=day; System.out.println("此天是第:"+maxday+"天"); } } 输入后别的都正确 但当输入某年 13月 几号时 月份没有报错 是哪里有问题? 里面该怎么办?希望尽快回答!~

public class Sun9 { public static void main(String[] args) { Scanner in =new Scanner(System.in); System.out.print("请输入年份:"); int year =in.nextInt(); System.out.print("请输入月份:"); int mouth = in.nextInt(); System.out.print("请输入几号:"); int day = in.nextInt(); int maxday=0; for(int i=1;i<mouth;i++){ if(mouth>12) { year++; mouth-=12; } if(mouth<1) { year--; mouth=12-mouth; } switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: maxday+=31; break; case 4: case 6: case 9: case 11: maxday+=30; break; case 2: maxday+=28; break; default: System.out.println("输入月份错误!"); } } if((mouth)>2&&((year%4==0)&&year%100!=0)||(year%400==0)){ maxday++; } maxday+=day; System.out.println("此天是第:"+maxday+"天"); } }
希望采纳
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-06-22
当你输入13月时,你的for(int i=1;i<mouth;i++)这个循环里面i的值只能为12,当然不会报错,如果你把i<mouth改成i<=mouth就会报错了.
相似回答