在Java中打印出日历表

时间:2018-11-13 04:14:08

标签: java

这是我用来打印出Java日历表的代码。 这个问题很容易解决,我确实获得了必要的代码。

    public static void printMonthTitle(int year, int month) {
        System.out.println("Sun Mon Tue Wed Thu Fri Sat");
    }

    public static void printMonthBody(int year, int month) {
        int startDay = getStartDay(year, month);
        int numberofDaysInMonth = getNumberOfDaysInMonth(year, month);

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

        for (i = 1; i <= numberofDaysInMonth; i++) {
            System.out.printf("%2d", i);

            if ((i + startDay) % 7 == 0)
                System.out.println();
            else
                System.out.print("  "); // I'm having presentation errors written out, this is where I'm having trouble with

如果我输入2013 & 1作为输入,将出现以下结果:

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  30  31  

我解释了我已经编写的其他代码,并且试图删除第31天之后出现的两个空格,但是我遇到了麻烦。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int year = sc.nextInt();
    int month = sc.nextInt();

    printMonth(year, month);
}

public static void printMonth(int year, int month) {
    printMonthTitle(year, month);
    printMonthBody(year, month);
}

public static void printMonthTitle(int year, int month) {
    System.out.println("Sun Mon Tue Wed Thu Fri Sat");
}

public static void printMonthBody(int year, int month) {
    int startDay = getStartDay(year, month);
    int numberofDaysInMonth = getNumberOfDaysInMonth(year, month);

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

    for (i = 1; i <= numberofDaysInMonth; i++) {
        System.out.printf("%2d", i);

        if ((i + startDay) % 7 == 0)
            System.out.println();
        else
            System.out.print("  ");
    }
}

public static int getStartDay(int year, int month) {
    final int START_DAY_OF_JAN_1_1800 = 3;
    int totalNumberofDays = getTotalNumberofDays(year, month);
    return (totalNumberofDays + START_DAY_OF_JAN_1_1800) % 7;
}
public static int getTotalNumberofDays(int year, int month) {
    int total = 0;
    for (int i = 1800; i < year; i++)
        if (isLeapYear(i))
            total = total + 366;
        else
            total = total + 365;
    for (int i = 1; i < month; i++)
        total = total + getNumberOfDaysInMonth(year, i);

    return total;
}
public static int getNumberOfDaysInMonth(int year, int month) {
    if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        return 31;
    if (month == 4 || month == 6 || month == 9 || month == 11)
        return 30;
    if (month ==2) return isLeapYear(year) ? 29 : 28;

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

}

3 个答案:

答案 0 :(得分:0)

如果最后一个空格有问题,则可以用以下内容替换“ else”条件:

public static void printMonthBody(int year, int month)
  {
    int startDay = 2;
    int numberofDaysInMonth = 31;

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

    for (i = 1; i <= numberofDaysInMonth; i++)
    {
      System.out.printf("%2d", i);

      if ((i + startDay) % 7 == 0)
        System.out.println();
      else if(i != numberofDaysInMonth)
        System.out.print("  ");
    }
  }

答案 1 :(得分:0)

要避免最后2个空格,只需添加一个条件:

for (i = 1; i <= numberofDaysInMonth; i++) {
    System.out.printf("%2d", i);

    if ((i + startDay) % 7 == 0) {
        System.out.println();
    } else if (i != numberofDaysInMonth) {    // add condidiion
        System.out.print("  ");
    }
}

此外,作为提示,我想分享一种使用numberOfDayInMonth实现startDayJava 8 java.time的更新方法:

static int getNumberOfDaysInMonth(int year, int month) {
    YearMonth yearMonthObject = YearMonth.of(year, month);
    return yearMonthObject.lengthOfMonth();
}

static int getStartDay(int year, int month) {
    LocalDate date = LocalDate.of(year, month, 1);
    return date.getDayOfWeek().getValue();
}

答案 2 :(得分:0)

您只需在最后一天之前停止循环,然后在循环之外打印最后一天

public static void printMonthBody(int year, int month) {
    int startDay = getStartDay(year, month);
    int numberofDaysInMonth = getNumberOfDaysInMonth(year, month);

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

    for (i = 1; i <= numberofDaysInMonth-1; i++) { // stop one day before
        System.out.printf("%2d", i);

        if ((i + startDay) % 7 == 0)
            System.out.println();
        else
            System.out.print("  ");
    }
    System.out.printf("%2d", i); // print last day without trailing spaces
}