package DoWhile;import java.util.Scanner;public class Test_02 {
// 输入年月日,显示闰年-平年,并显示当天是该年第几天
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do{
System.out.println("请输入年份:");
int year = sc.nextInt();    //1、限定年月日开头不能输入0,且月份不能大于12,号数不能大于31
System.out.println("请输入月份:");   //2、for用来累计第几天的
int month = sc.nextInt();    //3、因为2月有28 和29天之分,能被4整数的叫闰
//1\3\5\7\8\10\12有31天;4\6\9\11有30天
System.out.println("请输入号数:");
int day = sc.nextInt();
int days = 0;
int count = 0;

if(year>0 && month>0 && day>0  && month<13 && day<32){
for(int i=1;i<month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days =31;
break;
case 4:
case 6:
case 9:
case 11:
days =30;
break;
case 2:
if((year%4==0 && year%100!=0) || (year%400==0)){
//    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
/*day = 29;
}else{
day = 28;
}*/
if(day >28){
day = 29;
days =29;
}else{
day = 28;
days =29;
}
}
break;
}//switch
count = count + day;
}//for
count = count + day;
//System.out.println(day);
//System.out.println(days);
if( (year%4==0 && year%100!=0) || (year%400==0) && day >=29 && days>=29){//用于 判断2月份28|29天 ,闰年2月是29天 平年2月是28天.
System.out.println(year+"年"+month+"月"+day+"日"+"本月天数有"+days+"天;是闰年"+"是该月的第"+count+"天");
}else{
System.out.println(year+"年"+month+"月"+day+"日"+"本月天数有"+days+"天;是平年"+"是该月的第"+count+"天");
}
}else{//大if的
System.out.println("输入异常!");
}
} while (sc.next().charAt(0) == 'y'); // do
}
}

解决方案 »

  1.   

    java.util.Calendar可以满足你的需求
      

  2.   

    Quote: 引用 1 楼 AlbertLiangzt 的回复:
    嗯是哪里错了呢?
      

  3.   

    写的比较粗糙,你可以优化一下 public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    do{
    System.out.println("请输入年份:");
    int year = sc.nextInt();    //1、限定年月日开头不能输入0,且月份不能大于12,号数不能大于31
    System.out.println("请输入月份:");   //2、for用来累计第几天的
    int month = sc.nextInt();    //3、因为2月有28 和29天之分,能被4整数的叫闰
    //1\3\5\7\8\10\12有31天;4\6\9\11有30天
    System.out.println("请输入号数:");
    int day = sc.nextInt();
    int days = 0;
    String leap = "";
    boolean isLeap ;
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    isLeap = true;
    leap = "闰年";
    }else{
    isLeap = false;
    leap = "平年";
    }
    if(year>0 && month>0 && day>0  && month<13 && day<32){
    switch(month){
    case 12:days += 30;
            case 11:days += 31;
            case 10:days += 30;
            case 9:days += 31;
            case 8:days += 31;
            case 7:days += 30;
            case 6:days += 31;    
            case 5:days += 30;
            case 4:days += 31;
            case 3: if (isLeap) {
             days += 29;
             }else{
             days += 28;
             }
            case 2:days += 31;
            case 1:days += day;
    }
    if( (year%4==0 && year%100!=0) || (year%400==0) && day >=29 && days>=29){//用于 判断2月份28|29天 ,闰年2月是29天 平年2月是28天.
    System.out.println(year+"年"+month+"月"+day+"日,是"+leap+",是当年的第"+days+"天");
    }else{
    System.out.println(year+"年"+month+"月"+day+"日,是"+leap+",是当年的第"+days+"天");
    }
    }else{//大if的
    System.out.println("输入异常!");
    }
    } while (sc.next().charAt(0) == 'y'); // do
    }
      

  4.   

    为啥用到 +=的?  days也是0 ,加有没意义嘛?
      

  5.   

            case 3: if (isLeap) {          days += 29;          }else{          days += 28;          }
    这个是用来判断case2的吧
      

  6.   


    days += 30    是days = days + 30 的简写
      

  7.   

    建议你看下switch case的用法,3月份的时候才会把2月份的28天或者29天加上,所以得在二月之后的时候加上你可以倒着看,
    一月份的日期,days = day
    二月份的时候,days = 31+day
    三月份的时候,days =  31 + 28(或者29) + day