基于菜单的日历系统

时间:2016-12-07 16:15:56

标签: java date menu calendar

在课程I中,我们的任务是创建一个基于菜单的日历系统,该系统将接受用户输入(它们指定日期)。他们通过输入数字来指定年份,月份,日期(例如26)和星期几。我不允许使用基于日期的库或内置类。

基于此,程序将需要根据输入输出日历,类似于:

Calendar for September 2016
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  -

如果月份有28天,那一天之后的任何一天都应显示为连字符( - ),如果该月份的第1天是星期一,则该星期日之前的星期日应显示为连字符而不是负数。我有一个数组,其中包含每个月的最大天数,可以在这里使用。

我不知道如何输出这样格式化的日历,因为它需要从用户定义的日期开始,然后返回到月初并转发到月末。

如上所述,有关如何格式化日历的任何帮助都将不胜感激。

我发现this有点类似,但没有考虑到用户选择了星期几。所以我遇到的主要问题是让日历将日期与星期几对齐。 更新: 我对日历的逻辑进行了所有数据验证(我认为)。它是:

 // Inputs user's choice for year
                System.out.print("Enter Year after 1999:" + " ");
                year = input.nextInt();

                // Data validation to ensure year is later than 1999
                if (year <= 1999) {
                    while (year <= 1999) {
                        System.out.println("Please enter a year after 1999: ");
                        year = input.nextInt();
                    }
                }

                // Checking if year is a leap year
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 && year % 100 ==0) {

                    // Changes Number of Days in February
                    monthdays[1] = 29;
                    leapyear     = true;
                } else {

                    // Changes Number of Days in February
                    monthdays[1] = 28;
                }

                // Inputs user's choice for month
                System.out.print("Enter Month (1 to 12):" + " ");
                month = input.nextInt();

                // Data validation to ensure selected month is between 1 and 12
                if ((month > 12) || (month < 1)) {
                    while ((month > 12) || (month < 1)) {
                        System.out.println("Please enter a month between 1 and 12: ");
                        month = input.nextInt();
                    }
                }

                // Inputs user's choice for day
                System.out.print("Enter Day:" + " ");
                day = input.nextInt();

                // Data validation to ensure the day is between 1 and the maximum number of days in that month (based on values contained in the array)
                if ((day > monthdays[month - 1]) || (month < 1)) {
                    while ((day > monthdays[month - 1]) || (month < 1)) {
                        System.out.print("Please enter a valid day for your chosen month: ");
                        day = input.nextInt();
                    }
                }

                // Inputs user's choice for day of week
                System.out.print("Enter day of week (1-7):" + " ");
                weekday = input.nextInt();

                // Data validation to ensure the weekday is between 1 and 7
                if ((weekday > 7) || (weekday < 1)) {
                    while ((weekday > 7) || (weekday < 1)) {
                        System.out.println("Please enter a day between 1 and 7:" + " ");
                        weekday = input.nextInt();
                    }
                }

                System.out.println("Thank you for your input. You will now be returned to the main menu.");

1 个答案:

答案 0 :(得分:0)

就菜单而言,使用java.io扫描程序。对于日历的逻辑,您将需要一周中的几天的字符串数组(7),一年中几个月的字符串数组(12)。您需要以某种方式考虑的一些知识是在9月,4月,6月和11月有30天。 1月,3月,5月,7月,8月,10月和12月有31天。和2月份的28天一样,但是没有获得2月份为29天的闰年。 Y2K也可能会参与逻辑。还知道可以被100整除的年份不是闰年,除了可以被400整除的年份。可能还有其他一些因素,但这应该对你有所帮助。

相关问题