当前位置: 代码迷 >> Java相关 >> 帮我找出错误所在,谢了。。
  详细解决方案

帮我找出错误所在,谢了。。

热度:281   发布时间:2013-04-02 18:47:52.0
帮我找出错误所在,谢了。。
以下代码就是个月历表,运行可以,就是达不了理想的功能,可以告诉我错在哪吗??

import java.util.*;

public class PrintCalendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        System.out.print("请输入年份:");
        int year = input.nextInt();
        
        System.out.print("\n请输入月份:");   
        int month = input.nextInt();
        
        printMonth(year, month);
    }   
   
    public static void printMonth(int year, int month) { //输出月历
        printMonthTitle(year, month);
        printMonthBody(year, month);
    }
   
    public static void printMonthTitle(int year, int month) { //输出月历头
        System.out.println("           " + year+"  "+getMonthName(month) );
        System.out.println("----------------------------");
        System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
    }
   
    public static void printMonthBody(int year, int month) { //输出月历体
        int startDay = getStartDay(year, month);
        int daysInMonth = getDaysInMonth(year, month);
        int i;
        
        for (i=0; i<startDay; i++) {
            System.out.print("    ");   
        }
        
        for (i=1; i<=daysInMonth; i++) {
            System.out.printf("%4d", i);
            
            if ( (i + startDay) % 7 == 0    ) {
                System.out.println();   
            }
        }
         
        System.out.println();
        
    }
   
    public static String getMonthName(int month) { //返回月名
        switch (month) {
            case 1 : return "一月";   
            case 2 : return "二月";
            case 3 : return "三月";
            case 4 : return "四月";
            case 5 : return "五月";
            case 6 : return "六月";
            case 7 : return "七月";
            case 8 : return "八月";
            case 9 : return "九月";
            case 10 : return "十月";
            case 11 : return "十一月";
            case 12 : return "十二月";
        }
        
        return " ";
    }   
   
    public static int getStartDay(int year, int month) { //返回月初的星期数
        final int START_DAY_FOR_JAN_1_1800 = 3;
        int totalNumberOfDays = getTotalNumberOfDays(year, month);
        
        return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
    }
   
    public static int getTotalNumberOfDays(int year, int month) { //返回1800年至月历年的天数
        int i, total = 0;
        
        for (i=1800; i<year; i++) {
            if ( leapYeay(year) ) {
                total = total + 366;   
            }   
            
            else {
                total = total + 365;   
            }
        }
        
        for (i=1; i<month; i++) {
            total = total + getDaysInMonth(year, i);   
        }
        
        return total;
    }
   
    public static int getDaysInMonth(int year, int month) { //返回该月天数
        switch (month) {
            case 1 :
            case 3 :
            case 5 :
            case 7 :
            case 8 :
            case 10 :
            case 12 : return 31;
            case 4 :
            case 6 :
            case 9 :
            case 11 : return 30;
            case 2 : return leapYeay(year) ? 29 : 28;     
        }
        
        return 0;
    }  
   
    public static boolean leapYeay(int year) { //判断是否为闰年
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
}
搜索更多相关的解决方案: static  void  public  import  

----------------解决方案--------------------------------------------------------
没人吗?

----------------解决方案--------------------------------------------------------
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7; 判断星期数 应该是  return (totalNumberOfDays % 7 + START_DAY_FOR_JAN_1_1800);
for (i=1800; i<year; i++) {
            if ( leapYeay(1800+i) ) {//这个地方你要判断每年是否是汝南,而不是单单判断输入的那年是否是闰年,
                total = total + 366;   
            }   
            
            else {
                total = total + 365;   
            }
        }
----------------解决方案--------------------------------------------------------
暂时发现这两个地方,试试看能不能正确。
----------------解决方案--------------------------------------------------------
我知道错在哪了 。。   
  for (i=1800; i<year; i++) {
             if ( leapYeay(year) ) { //这部分应该改为if ( leapYeay(i) ),因为i是1800起步的,谢谢解答了。。
                 total = total + 366;   
            }
        }   
----------------解决方案--------------------------------------------------------
  相关解决方案