从给定的月/年打印日历

时间:2014-07-31 23:55:21

标签: java calendar

我正在进行Java分配,它涉及在用户指定月份和年份后打印日历。我不能使用Calendar或GregorianCalendar类。我的问题是,日历在周六的第一天没有正确打印数月。我现在看了我的代码大约一个小时,我不确定出了什么问题。我正在使用Zeller的同余来查找月份的第一天,用“h”表示。

例如,2008年3月(不正确)的日历如下所示:

     March 2008
Su Mo Tu We Th Fr Sa
 1 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 31 

这是我的代码:

package calendar;

import java.util.Scanner;

public class Calendar
{
    private static int numDays = 0;
    private static int h = 0;
    public static boolean leap(int year)
    {
        if(((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static void firstDayOfYear(int year)
    {
        int month = 13;
        year--;
        h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
        String dayName = "";
        switch(h)
        {
            case 0: dayName = "Saturday"; break;
            case 1: dayName = "Sunday"; break;
            case 2: dayName = "Monday"; break;
            case 3: dayName = "Tuesday"; break;
            case 4: dayName = "Wednesday"; break;
            case 5: dayName = "Thursday"; break;
            default: dayName = "Friday"; break;
        }
        System.out.println("The first day of the year is " + dayName);
    }
    public static void firstDayOfMonth(int month, int year)
    {
        if(month == 1 || month == 2)
        {
            month += 12;
            year--;
        }
        h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7;
        String dayName = "";
        switch(h)
        {
            case 0: dayName = "Saturday"; break;
            case 1: dayName = "Sunday"; break;
            case 2: dayName = "Monday"; break;
            case 3: dayName = "Tuesday"; break;
            case 4: dayName = "Wednesday"; break;
            case 5: dayName = "Thursday"; break;
            default: dayName = "Friday"; break;
        }
        System.out.println("The first day of the month is " + dayName);
    }
    public static void numDaysInMonth(int month, int year)
    {
        int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        if (month == 2 && leap(year)) days[month] = 29;
        numDays = days[month];
        System.out.println("The number of days in the month is " + numDays);
    }
    public static void printCal(int month, int year)
    {
        String[] monthNames = {"","January","February","March","April","May","June","July","August","September","October","November","December"};

        System.out.println("    " + monthNames[month] + " " + year);
        System.out.println("Su Mo Tu We Th Fr Sa");
        for (int i = 0; i < h - 1; i++)
            System.out.print("   ");
        for (int i = 1; i <= numDays; i++)
        {
            System.out.printf("%2d ", i);
            if (((i + h - 1) % 7 == 0) || (i == numDays)) System.out.println();
        }
    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter month (1-12): ");
        int month = input.nextInt();
        if(month < 1 || month > 12)
        {
            System.out.println("Invalid month. Valids inputs are 1-12.");
            System.exit(0);
        }
        System.out.print("Enter year: ");
        int year = input.nextInt();
        if(year < 1753)
        {
            System.out.println("Invalid year. Valid inputs are 1753 and beyond.");
            System.exit(0);
        }
        if(leap(year))
        {
            System.out.println(year + " is a leap year.");
        }
        else
        {
            System.out.println(year + " is NOT a leap year.");
        }
        firstDayOfYear(year);
        firstDayOfMonth(month, year);
        numDaysInMonth(month, year);
        printCal(month, year);
    }    
}

4 个答案:

答案 0 :(得分:2)

您的代码对every month that starts with a Saturday会有同样的问题。这意味着问题可能在这一行 -

for (int i = 0; i < h - 1; i++)
  System.out.print("   ");

h设为7而不是0,这将为您解决问题。您可以在此处修复此问题,也可以从1到7而不是0到6启动h,然后进行其他必要的更改。

答案 1 :(得分:1)

问题是您的索引搞砸了。这意味着你是从1而不是0开始弱者的月和日。但是这里

h = (1 + (int)(((month + 1) * 26) / 10.0) + year + (int)(year / 4.0) + 6 * (int)(year / 100.0) + (int)(year / 400.0)) % 7

最后的%7让它从0开始!这会导致循环

for (int i = 0; i < h - 1; i++)
  System.out.print("   ");

for (int i = 0; i < -1; i++)
  System.out.print("   ");

因为周六的h为0。

要解决此问题,您应该使用0启动函数中的所有索引。然后,当您将用户输入索引为1时,只需减去1并将其传递给函数。那么你也不会在所有数组前面有那个空白条目(顺便说一句,这应该是类顶部的静态终结)。

答案 2 :(得分:1)

我知道你得到了答案。但是这里有一个快速修复(抱歉它有点hacky),你可以合并来修改星期六的索引,而不需要对你的代码进行很多更改。

System.out.println("Su Mo Tu We Th Fr Sa");
        int xx = h == 0 ? 7 : h; // Correct the index for Saturday.
        for (int i = xx; i > 1; i--) // Reversing the loop condition
            System.out.print("   ");
        for (int i = 1; i <= numDays; i++) {
            System.out.printf("%2d ", i);
            if (((i + h - 1) % 7 == 0) || (i == numDays))
                System.out.println();
        }

答案 3 :(得分:0)

这将解决闰年问题

if ((((i + h) % 7 == 0) || (i == numDays) && leap(year)))