如何在java中显示日历

新手上路,请多包涵

我目前正在做一个问题集,我必须创建一个日历来显示一年中的所有月份,包括其中的月份日期。但是我对每个月第一行的间距有疑问。在课堂上我们只学过switch语句、if、else、while、do-while、for循环

这是我的一个月中当前显示的内容:

输出 图片图中没有显示我的输入,但我写的是 2016 年和 5 年开始的工作日。

所需内容的输出图像 再次,所需内容 的图片。我认为我的问题可能是我使用的方程式:int firstDayEachMonth = (daysMonth + firstDayYear)%7;虽然老师把这个方程式给了我们用,但是好像行不通。

如您所见,第一行的空格一直向左,它应该与指定日期对齐,在这种情况下,1 月,1 月 1 日应与星期五对齐,1 月 2 日应与星期六对齐,但目前是周日和周一。

     import java.util.Scanner;

    public class DisplayCalendar
       {
        public static void main(String[] args)
        {
        //Create a new scanner
        Scanner input = new Scanner(System.in);

        // Prompt user to enter year
        System.out.print("Enter a year: ");
        int year = input.nextInt();

        // Prompt user to enter first day of the year
        System.out.print("Enter the weekday that the year starts: ");
        int firstDayYear = input.nextInt();

        // A for loop that prints out each month
        for(int month = 1; month <= 12; month++)
        {
            // Set the value of the amount of days in a month
            int daysMonth = 0;

            // Set value of the month
            String monthDisplay = "";

            // Find name of each month and number of days
            switch(month)
            {
                case 1: monthDisplay = "January";
                    daysMonth = 31;
                    break;

                case 2:
                    monthDisplay = "February";
                    int leapYear = 0;
                    while (leapYear > -1)
                    {
                        // Count all years that are divisible by 4 to be a leap year.
                        leapYear += 4;

                        // If the year inputted is a leap year, the days of the month will be 29.
                        if (year == leapYear)
                        {
                            daysMonth = 29;
                            break;
                        }

                        else
                        {
                            daysMonth = 28;
                        }
                    }
                    break;

                case 3: monthDisplay = "March";
                    daysMonth = 31;
                    break;

                case 4: monthDisplay = "April";
                    daysMonth = 30;
                    break;

                case 5: monthDisplay = "May";
                    daysMonth = 31;
                    break;

                case 6: monthDisplay = "June";
                    daysMonth = 30;
                    break;

                case 7: monthDisplay = "July";
                    daysMonth = 31;
                    break;

                case 8: monthDisplay = "August";
                    daysMonth = 31;
                    break;

                case 9: monthDisplay = "September";
                    daysMonth = 30;
                    break;

                case 10: monthDisplay = "October";
                    daysMonth = 31;
                    break;

                case 11: monthDisplay = "November";
                    daysMonth = 30;
                    break;

                case 12: monthDisplay = "December";
                    daysMonth = 31;
                    break;

                // If the month is not recognized, dialog box will be displayed, and then exits program.
                default : System.out.print("Invalid: Your month is not recognized. ");
                    System.exit(0);

            }
            // Display the month and year
            System.out.println("                      "+ monthDisplay + " " + year);

            // Display the lines
            System.out.println("_____________________________________");

            // Display the days of the week
            System.out.println("Sun     Mon     Tue     Wed     Thu     Fri     Sat");

            // Print spaces depending on the day the month starts.
            int firstDayEachMonth = (daysMonth + firstDayYear)%7;
            for (int space = 1; space <= firstDayEachMonth; space++)
                System.out.print("   ");

            // Print the days
            for (int daysDisplay = 1; daysDisplay <= daysMonth; daysDisplay++)
            {
                if (firstDayYear%7 == 0)
                    System.out.println();

                System.out.printf("%3d      ", daysDisplay);
                firstDayYear += 1;
            }
            System.out.println();
        }

    }
}

我们不能使用不同的库(如 Calendar ),只能与扫描仪保持一致。

原文由 Flinze 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 334
2 个回答

你能试试这个例子吗?我可以看到以下输出:

    February 2016
   Sun  Mon Tue   Wed Thu   Fri  Sat
        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

 package general;

import java.util.Scanner;

public class DisplayCalendar {

    public static void main(String[] args) {
        int Y = 2016;    // year
        int startDayOfMonth = 5;
        int spaces = startDayOfMonth;

        // startDayOfMonth

        // months[i] = name of month i
        String[] months = {
                "",                               // leave empty so that we start with months[1] = "January"
                "January", "February", "March",
                "April", "May", "June",
                "July", "August", "September",
                "October", "November", "December"
            };

            // days[i] = number of days in month i
            int[] days = {
                0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
            };

            for (int M = 1; M <= 12; M++) {

            // check for leap year
            if  ((((Y % 4 == 0) && (Y % 100 != 0)) ||  (Y % 400 == 0)) && M == 2)
                days[M] = 29;


            // print calendar header
            // Display the month and year
            System.out.println("          "+ months[M] + " " + Y);

            // Display the lines
            System.out.println("_____________________________________");
            System.out.println("   Sun  Mon Tue   Wed Thu   Fri  Sat");

            // spaces required
               spaces = (days[M-1] + spaces)%7;

            // print the calendar
            for (int i = 0; i < spaces; i++)
                System.out.print("     ");
            for (int i = 1; i <= days[M]; i++) {
                System.out.printf(" %3d ", i);
                if (((i + spaces) % 7 == 0) || (i == days[M])) System.out.println();
            }

            System.out.println();
        }
    }
}

原文由 The Roy 发布,翻译遵循 CC BY-SA 4.0 许可协议

由于这似乎是一个作业,我不会费心给你正确的算法。那会打败你——或其他有同样问题的人——练习你的编程和分析技能的目的。

在这一行 for (int space = 1; space <= firstDayEachMonth; space++) 你可以完全忽略 firstDayEachMonth 结果并使用你的 firstDayYear 计数器。该计数器具有一年中的起始工作日,并且每天递增。此外,有必要定义您的一周是从 0 还是 1 开始。

在这部分中,您已经在此处为周末设置了换行符:

 if (firstDayYear%7 == 0)
   System.out.println();

当你达到这种情况时,我会重置 firstDayYear 因为因为你将它用作参数来设置你的空间,所以你永远不会让这个数字大于 7。这样你就可以正确安排每周的行在日历上,唯一的问题是在屏幕上以正确的格式显示它。

当您像这样打印星期几标题时 System.out.println("Sun Mon Tue Wed Thu Fri Sat"); 请注意,名称的长度为 3 加上 5 个空格。所以这一行 System.out.printf("%3d ", daysDisplay); 应该有一个宽度为 3 个空格的数字,这解释了 %3d 的使用,加上 5 个空格。在这种情况下,你有 6 个空格,你总是给你错误的偏移量,并且会在某些行上造成一些麻烦。

这些是我注意到的事情,希望对您有所帮助。和平!

原文由 Luan Moraes 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题