回 帖 发 新 帖 刷新版面

主题:求救

我想编个日历大家看下错误最后的两个for循环有问题就是不知道错在哪?侠士救命!
import java.util.*;
public class Zuoye {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int days=0;
        int sum=0;
    Scanner s=new Scanner(System.in);
    System.out.print("请输入年份:\n");
    int year=s.nextInt();
    System.out.print("请输入月份:\n");
    int yue=s.nextInt();
    if(year%4==0&&year%100!=0||year%400==0){
        System.out.println(year+"\t是闰年");
    }else{
        System.out.println(year+"\t是平年");
    }
    switch(yue){
    case 4:
    case 6:
    case 9:
    case 11:
        System.out.println(yue+"\t共30天");
        days=30;
        break;
    case 2:
        if(year%4==0&&year%100!=0||year%400==0){
            System.out.println(yue+"\t共29天");
            days=29;
        }else{
            System.out.println(yue+"\t共28天");
            days=28;
        }
        break;
        default:
            System.out.println(yue+"\t共31天");
            days=31;
    }
         for(int i=1900;i<year;i++){
             if(i%4==0&&i%100!=0||i%400==0){
                 sum=sum+366;
             }else{
                 sum=sum+365;
             }
         }
         switch(yue){
         case 4:
         case 6:
         case 9:
         case 11:
             sum=sum+30;
             break;
         case 2:
             if(year%4==0&&year%100!=0||year%400==0){
                 sum=sum+29;
             }else{
                 sum=sum+28;
             }
             break;
             default:
                 sum=sum+31;
         }
         int xingqi=1+sum%7;
         for(int i=0;i<7;i++){
             System.out.print("星期"+i+"\t");
         }
         for(int nullNo=0;nullNo<xingqi;nullNo++){
             System.out.print("\t");
         }
         for(int j=1;j<=days;j++){
             if((j+sum)%7==1){
                System.out.println();
             }else{
                 System.out.print(j+"\t");
             }
         }
    }

}

回复列表 (共2个回复)

沙发

import java.util.*;
public class Zuoye {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int days=0;
        int sum=0;
    Scanner s=new Scanner(System.in);
    System.out.print("请输入年份:\n");
    int year=s.nextInt();
    System.out.print("请输入月份:\n");
    int yue=s.nextInt();
    if(year%4==0&&year%100!=0||year%400==0){
        System.out.println(year+"\t是闰年");
    }else{
        System.out.println(year+"\t是平年");
    }
    switch(yue){     
    case 4:
    case 6:
    case 9:
    case 11:
        System.out.println(yue+"\t共30天");
        days=30;
        break;
    case 2:
        if(year%4==0&&year%100!=0||year%400==0){
            System.out.println(yue+"\t共29天");
            days=29;
        }else{
            System.out.println(yue+"\t共28天");
            days=28;
        }
        break;
        default:
            System.out.println(yue+"\t共31天");
            days=31;
    }
         for(int i=1900;i<year;i++){
             if(i%4==0&&i%100!=0||i%400==0){
                 sum=sum+366;
             }else{
                 sum=sum+365;
             }
         }
         switch(yue){//月份加的天数不对。 如果是*年5月份,应该从+1月+2月+3月+4月算出来的才是5月1日的天数  而这里是+5月的天数
         case 4:
         case 6:
         case 9:
         case 11:
             sum=sum+30;
             break;
         case 2:
             if(year%4==0&&year%100!=0||year%400==0){
                 sum=sum+29;
             }else{
                 sum=sum+28;
             }
             break;
             default:
                 sum=sum+31;
         }
         int xingqi=1+sum%7;
         for(int i=0;i<7;i++){
             System.out.print("星期"+i+"\t");
         }  //在打印完列头星期几之后,啊是应该换个行先?
         for(int nullNo=0;nullNo<xingqi;nullNo++){
             System.out.print("\t");
         }    
         for(int j=1;j<=days;j++){
             if((j+sum)%7==1){   //若(j+sum)%7==1就不用打印这天么?    怎么只换行不打印天数?还有,根据楼主意思,应该是每到星期六换行,为什么会是 %7==1?
                System.out.println();
             }else{
                 System.out.print(j+"\t");
             }
         }
    }

}

板凳

我只是看了下你大概的思路,把我觉得有问题的地方写出来。  具体没帮你改(觉得自已改印象会深点   ^_^)。
另外,这种事事实上可以直接用java里util包下的Calendar类还做,就不用什么都要你算了,直接从类里面取出来

我来回复

您尚未登录,请登录后再回复。点此登录或注册