gpt4 book ai didi

java - 向前循环的问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:48 25 4
gpt4 key购买 nike

所以我的循环有问题,其目的是在移动到下一行之前填充整个几个月,就像这样

     January   2000        February   2000        March   2000   
|S M Tu W Th F S| |S M Tu W Th F S| |S M Tu W Th F S|
1 1 2 3 4 5 1 2 3 4
2 3 4 5 6 7 8 6 7 8 9 10 11 12 5 6 7 8 9 10 11
9 10 11 12 13 14 15 13 14 15 16 17 18 19 12 13 14 15 16 17 18
16 17 18 19 20 21 22 20 21 22 23 24 25 26 19 20 21 22 23 24 25
23 24 25 26 27 28 29 27 26 27 28 29 30 31

但我的结果看起来像这样 -

          January   2000        February   2000        March   2000   
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 1 2 3 4 5 1 2 3 4
April 2000 May 2000 June 2000
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 1 2 3 4 5 6 1 2 3
July 2000 August 2000 September 2000
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 1 2 3 4 5 1 2
October 2000 November 2000 December 2000
|S M Tu W Th F S||S M Tu W Th F S||S M Tu W Th F S|
1 2 3 4 5 6 7 1 2 3 4 1 2

可以做什么或添加什么来使其充分发挥作用并顺利通过?我觉得我有一个循环问题,但我真的无法确定它

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) {

// take in command line argument to determine the month and year
int month = 0;
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 < 12;) {

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

// print calendar header
for (int f = 0; f < 3; f++) {

System.out.print(" " + months[month] + " " + year + " ");
month++;
}
System.out.println();
for (int f = 0; f < 3; f++) {
System.out.print("|S M Tu W Th F S|");
}
System.out.println();

// print the calendar
month -= 3;
for (int f = 0; f < 3; f++) {
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])) {
if(month<12)
month++;
break;
}

}

}
System.out.println();

}

最佳答案

您缺少一个循环。

外循环从最左边的月份到最右边的月份(总共 3 个)。但这只会给你一行。

您必须用另一个循环来包裹它,该循环将遍历所有行。

关于java - 向前循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53661300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com