多维数组日历,如何解决简单问题

时间:2018-03-05 19:15:08

标签: java java-time

我正在尝试修复我的日历(月 - 三月):

Mon Tue Wed Thu Fri Sat Sun 
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   0   0   0   
32  0   0   0   0   0   0   

1)第一个问题是我不能在31之后停止计数,因为你可以看到我有0 0 0 0 0但是在这个地方我希望有空的空间。

2)第二个问题是,3月是从星期四开始,而不是从星期一开始,如何从星期四开始计算天数?这是我的代码:

public class MyPoorCalendar{
    public static void main(String[] args) {
        int firstday = 1;
        int mycalendar[][] = new int[6][7];
        String nameOfTheWeeks = "Mon\t" + "Tue\t" + "Wed\t" + "Thu\t" + "Fri\t" + "Sat\t" + "Sun\t";
        System.out.println(nameOfTheWeeks);

        //initializing
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                mycalendar[i][j] = firstday++;
                if (firstday > 31) {
                    break;
                }
            }
        }
        //printing
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(mycalendar[i][j] + "\t");
                if (j == 6) {
                    System.out.println();
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

当mycalendar [i] [j] == 0

时,只需打印一个空格
 System.out.print((mycalendar[i][j] == 0 ? " " : mycalendar[i][j]) + "\t");

设置值时,你应该突破最外面的for循环,目前你只是打破了最内层循环,所以这样做

   for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            mycalendar[i][j] = firstday++;
        }
        if (firstday > 31) {
                break;
        }
    }

最后,如果您想从日历中的其他位置开始,那么您可以放置​​一个计数器,当它等于0时,您可以开始您的日子。例如,如果你想在周四开始,你可以做这样的事情

   int dayToStartOn = 4; //Thursday
   for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            mycalendar[i][j] = dayToStartOn-- > 0 ? 0 : firstday++;
        }
        if (firstday > 31) {
                break;
        }
    }

答案 1 :(得分:1)

你可以这样做(代码中的解释):

// store the length of the month and the first day of month's weekday number
int lengthOfMonth = LocalDate.now().lengthOfMonth();
int firstDayOfMonthsWeekDay = LocalDate.now().withDayOfMonth(1).getDayOfWeek().getValue();

// use a labeled break statement to terminate the outer for loop when we reach the end of the month
month:
for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 7; j++) {
        if(--firstDayOfMonthsWeekDay > 0){ // fixing first day of month's weekday to start counting from tuesday for example
            continue;
        }

        mycalendar[i][j] = firstday++;
        if (firstday > lengthOfMonth) {
            break month;                   // the execution will continue after the outer loop
        }
    }
}

for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 7; j++) {
        System.out.print((mycalendar[i][j] == 0 ? " " : mycalendar[i][j]) + "\t");  // replace 0's in the array with spaces
        if (j == 6) {
            System.out.println();
        }
    }
}

输出:

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

有关标记中断here的更多信息。关于java.time api here