gpt4 book ai didi

2 列 Java 控制台日历

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

大家好,我真的需要你们的帮助。我需要以 3 列输出 3 年日历。第一列中的年份应该是用户输入的年份。

我有一个代码,可以发布 3 年日历,但仅在 1 列中。我需要在另一列中分隔另一年,请帮助我

package project;

import java.util.*;

public class les1 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Calendar cal = Calendar.getInstance();
String ans;
// getting the system calendar's year, maximum days,
// weeks per month and system calendar's month.
int year = cal.get(Calendar.YEAR);
int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int week = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int col[] = new int[3];

do {
// validation for input year

do {
System.out.print("\nEnter year: ");
while (true)
try {
year = Integer.parseInt(in.next());
break;
} catch (Exception ex) {
System.out.println("Try again!");
System.out.print("\nEnter year: ");
}
if (year < 1900 || year > 2099) {
System.out.println("Try again!");
}
} while (year < 1900 || year > 2099);

/*
* System.out.println("enter number of elements");
*
* int n = in.nextInt();
*
* int arr[] = new int[n];
*
* for (int j = 0; j < n; j++) {// for reading array
*
* }
*
* for (int j : arr) {
*/
// loop for months
for (month = 1; month <= 1; month++) {
// calculating years, month, days and weeks.
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 = (1 + x + (31 * m) / 12) % 7;
// calculate leap year
boolean LeapYear = (year % 400 == 0);
// displaying month as String
cal.set(year, month, 0);
System.out
.print(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH) + "\t\t\t\t\t\t");
System.out.print("Sun Mon Tue Wed Thu Fri Sat" + "\t\t\t");

int length = (int) (30 + ((month + (month / 8.0)) % 2));
if (month == 2) {
if (LeapYear) {
length -= 1;
} else {
length -= 2;
}
}

for (int a = 0; a < col.length; a++) {
int counter = 1;
// spacing for first day of the month
for (int i = 0; i < d; i++) {
System.out.print(" ");
counter++;
}
// spacing for days
for (day = 1; day <= length; day++) {
System.out.printf("%2d", day);
System.out.print((counter++ % 7 != 0) ? " " : "\n");
}
}
week = (week + length) % 6;
}
year++;
/* } */
System.out.print("\n\n" + "Enter another year (Y/N)? ");
ans = in.next();
} while (ans.equals("Y") || ans.equals("y"));
System.out.println("\nEnd!");
in.close();
}
}

最佳答案

看一下下面的代码。它将在三列中打印三个不同年份的日历。我用过this确定任何月份中星期几的公式。

 import java.util.*;

public class les1 {

public static void main(String[] args) {

String[] days = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
Scanner in = new Scanner(System. in );
Calendar cal = Calendar.getInstance();
String ans;
int year = cal.get(Calendar.YEAR);
int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int week = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int col[] = new int[3];

do {
// validation for input year

do {
System.out.print("\nEnter year: ");
while (true)
try {
year = Integer.parseInt( in .next());
break;
} catch (Exception ex) {
System.out.println("Try again!");
System.out.print("\nEnter year: ");
}
if (year < 1900 || year > 2099) {
System.out.println("Try again!");
}
} while (year < 1900 || year > 2099);

for (month = 1; month <= 1; month++) {


HashMap < Integer, Integer > map = new HashMap < Integer, Integer > ();
map.put(1, 0);
map.put(7, 6);
map.put(2, 3);
map.put(8, 2);
map.put(3, 3);
map.put(9, 5);
map.put(4, 6);
map.put(10, 0);
map.put(5, 1);
map.put(11, 3);
map.put(6, 4);
map.put(12, 5);

System.out.println();

for (int a = 0; a < col.length; a++) {
int counter = 1;
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
System.out.printf("\t\t\t"); //Formatting for days

}
// System.out.println();

// calculate leap year
boolean LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;
// displaying month as String

ArrayList < Integer > dds = new ArrayList < Integer > ();
dds.add(0);
dds.add(0);
dds.add(0);
dds.add(0);
int indexofA = 0;

String yy = year + "";

int m;
if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);

int y = Integer.parseInt(yy.substring(2, 4));
int c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
int x = y + y / 4 + c;
int d = (1 + x + m) % 7;
// cal.set(year, month, 0);

int length = (int)(30 + ((month + (month / 8.0)) % 2));
if (month == 2) {
if (LeapYear) {
length -= 1;
} else {
length -= 2;
}
}
System.out.print("len : " + length);
System.out.println();
boolean thirtyone = false;
int count = 0;
for (int r = 0; r < 6; r++) { //For each row of the calendars

String[] dayss = new String[7];

int index = 0;

for (int a = 0; a < col.length; a++) { //For each column of a calendar

for (int i = 0; i < d && dds.get(indexofA) == 0; i++) {

dayss[index] = " ";
index++;
}

for (day = dds.get(indexofA) + 1; day <= length; day++) {
if (day == 31) {
dayss[index] = day + "";
index++;
thirtyone = true;

} else if (day < 31) {
dayss[index] = day + "";
index++;
}

if (index == 7) {
dds.set(indexofA, day);
break;
}

}

indexofA++;
System.out.printf("%-6s%-6s%-6s%-6s%-6s%-6s%-6s", dayss[0], dayss[1], dayss[2], dayss[3],
dayss[4], dayss[5], dayss[6]);
System.out.printf("\t\t\t");

index = 0;
year++;
LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;
yy = year + "";
if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);

y = Integer.parseInt(yy.substring(2, 4));
c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
x = y + y / 4 + c;
d = (1 + x + m) % 7;

}
year -= 3;
yy = year + "";
LeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) LeapYear = true;

if (LeapYear && month == 1) m = map.get(month) + 6;
else if (LeapYear && month == 2) m = map.get(month) - 1;
else m = map.get(month);

y = Integer.parseInt(yy.substring(2, 4));
c = (year / 100);
if (c % 4 == 0) c = 6;
else if (c % 4 == 1) c = 4;
else if (c % 4 == 2) c = 2;
else c = 0;
x = y + y / 4 + c;
d = (1 + x + m) % 7;

indexofA = 0;
System.out.println();

}

}
System.out.print("\n\n" + "Enter another year (Y/N)? ");
ans = in .next();
} while (ans.equals("Y") || ans.equals("y"));
System.out.println("\nEnd!"); in .close();
}

}

关于2 列 Java 控制台日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805982/

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