按给定的月份/年份打印日历 [英] Printing a Calendar from Given Month/Year

查看:55
本文介绍了按给定的月份/年份打印日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Java作业,它涉及在用户指定月份和年份之后打印日历。我不能使用Calendar或GregorianCalendar类。我的问题是日历在星期六的第一天无法正确打印月份。我已经看了大约一个小时的代码,但不确定出什么问题。我正在使用Zeller的Congruence查找以 h表示的月份的第一天。

I'm working on a Java assignment and it involves printing a calendar after the user specifies a month and a year. I cannot use the Calendar or GregorianCalendar classes. My problem is that the calendar does not correctly print months with their first day on a Saturday. I've looked at my code for about an hour now, and I'm not sure what went wrong. I'm using Zeller's Congruence to find the first day of the month, which is represented by "h".

例如,2008年3月的日历(错误地)看起来像这样:

For example, the calendar on March 2008 (incorrectly) looks like this:

     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);
    }    
}


推荐答案

您的对于从星期六开始的每个月,代码都将具有相同的问题。这意味着问题可能就在这行-

Your code will have the same issue for every month that starts with a Saturday. This means that the problem is probably in this line -

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

h 设置为7而不是0将为您修复它。您可以在此处解决此问题,或者可能需要将 h 从1改为7,而不是从0改为6,然后进行其他必要的更改。

Having h as 7 instead of 0 here will fix it for you. You can either fix that here or you may need to start h from 1 to 7 instead of 0 to 6 and make other required changes of course.

这篇关于按给定的月份/年份打印日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆