如何将多行输出转换为3x4

时间:2018-12-20 17:39:19

标签: java

我遇到一个问题,我需要在提供一年时打印年日历,并且可以逐月进行查询,但是如何将每行输出的1个月转换为每行输出的3个月?

我花了数小时的时间来摆弄一次处理多行并尝试输入到数组中的操作,但是由于某种原因,我无法使其中的任何一条正常工作。

public static int day(int month, int day, int year) {
    int y = year - (14 - month) / 12;
    int x = y + y / 4 - y / 100 + y / 400;
    int m = month + 12 * ((14 - month) / 12) - 2;
    int d = (day + x + (31 * m) / 12) % 7;
    return d;
}

public static boolean isLeapYear(int year) {
    if ((year % 4 == 0) && (year % 100 != 0))
        return true;
    if (year % 400 == 0)
        return true;
    return false;
}
public static void main(String[] args) {
    int year = 2000;
    String[] months = { "", // left empty so that 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 (month = 1; month < 13;) {

        // check for leap year
        if (month == 2 && isLeapYear(year))
            days[month] = 29;

        // print calendar header

            System.out.print("     " + months[month] + "   " + year + "   ");


        System.out.println();

            System.out.print("|S  M Tu  W Th  F  S|");

        System.out.println();

        // print the calendar
        int d = day(month, 1, year);
        for (int i = 0; i < d; i++)
            System.out.print("   ");
        for (int i = 1; i <= days[month]; i++) {
            System.out.printf("%2d ", i);
            if (((i + d) % 7 == 0) || (i == days[month]))
                System.out.println();

        }
        month++;
    }

day();只需输出每月的第一天,例如,2000年2月将输出相当于星期二的值

我的输出当前看起来像这样

         January   2000   
|S  M Tu  W Th  F  S|
                   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 
     February   2000   
|S  M Tu  W Th  F  S|
       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 
     March   2000   
|S  M Tu  W Th  F  S|
          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 
     April   2000   
|S  M Tu  W Th  F  S|
                   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 
     May   2000   
|S  M Tu  W Th  F  S|
    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 
     June   2000   
|S  M Tu  W Th  F  S|
             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 
     July   2000   
|S  M Tu  W Th  F  S|
                   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 
     August   2000   
|S  M Tu  W Th  F  S|
       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 
     September   2000   
|S  M Tu  W Th  F  S|
                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 
     October   2000   
|S  M Tu  W Th  F  S|
 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 
     November   2000   
|S  M Tu  W Th  F  S|
          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 
     December   2000   
|S  M Tu  W Th  F  S|
                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 

0 个答案:

没有答案