Java Basic日历打印

时间:2015-10-28 22:00:41

标签: java

我正在为我的计算机科学1课程完成一项任务,我被困在某一部分。编写代码以便用户输入年份,然后输入星期几(使用0到6)来启动日历。我将从我的教师骨架中获取代码。我目前的问题是,我无法在7天后获得打印新行的代码。提前谢谢!

import java.util.Scanner;
public class Test {

        /**
         * Determines if an input is a leap year
         * 
         * @param year year in question
         * @return true if a leap year
         */
    public static boolean isLeapYear(int year) {
        if((year % 4 == 0) || (year % 100 == 0)) return true;
        return false;
        }

        /**
         * Outputs a month to the console
         * 
         * @param month title
         * @param startDay 0=Sunday ... 6=Saturday
         * @param numDays number of days in the month
         * @return day of the week of the last day of the month
         */
        public static int printMonth(String month, int startDay, int numDays){
            System.out.println(month);
            int dayOfWeek = 10;

            for(int i = 0; i < startDay; i++){
                System.out.print("  ");
            }

            for(int i = 1; i < numDays; i++){
                System.out.printf("%2d ", i);
                if((i + startDay) % 7 == 0);{
                    System.out.print("");
                }
            }
            //Your code goes here
            System.out.println("Print: " + month);
            System.out.println("");

            return dayOfWeek;
        }

        /**
         * Program execution point:
         * input year, day of the week (0-6) of january 1
         * output calendar for that year
         * 
         * @param args command-line arguments (ignored)
         */
        public static void main(String[] args) {
            @SuppressWarnings("resource")
            final Scanner input = new Scanner(System.in);

            System.out.print("Enter the year: ");
            final int year = input.nextInt();

            System.out.print("Enter the day of the week of January 1st (0=Sunday, 1=Monday, ... 6=Saturday): ");
            final int firstDay = input.nextInt();

            if (year<=0) {
                System.out.println("The year must be positive!");
                System.exit(0);
            }

            if (firstDay<0 || firstDay>6) {
                System.out.println("The day of January 1st must be between 0 and 6!");
                System.exit(0);
            }

            final int numFebDays;
            if (isLeapYear(year)) {
                numFebDays = 29;
            } else {
                numFebDays = 28;
            }

            int lastDayOfWeek;
            lastDayOfWeek = printMonth("January", firstDay, 31);
            lastDayOfWeek = printMonth("February", lastDayOfWeek, numFebDays);
            lastDayOfWeek = printMonth("March", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("April", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("May", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("June", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("July", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("August", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("September", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("October", lastDayOfWeek, 31);
            lastDayOfWeek = printMonth("November", lastDayOfWeek, 30);
            lastDayOfWeek = printMonth("December", lastDayOfWeek, 31);

        }
}

1 个答案:

答案 0 :(得分:1)

此程序存在一些问题。注释中提到了其中两个,将System.out.print(&#34;&#34;)更改为System.out.println(&#34;&#34;),并删除;在for语句结束时。

此外,您不会更改dayOfWeek的值。它总是返回10,这就是它向右打印的原因。它应该是

int dayOfWeek = (startDay + numDays) % 7;

我还注意到计算闰年的公式略有不正确。一年是闰年,如果它可以被4整除,除非它是100可交付的,除非它是闰年,如果它可以被400整除。这里有一个有效的功能:

public static boolean isLeapYear(int year) {
    return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ;
}

我看到的另一个错误是该月的最后一天没有打印。通过更改for循环的这一部分来解决这个问题:i <= numDays;