gpt4 book ai didi

java - 使用 Java 打印 12 月年和接下来的 2 年

转载 作者:行者123 更新时间:2023-11-30 06:41:06 26 4
gpt4 key购买 nike

已经有了这个的初始程序,我使代码尽可能短和精确。但是经过检查,似乎接下来的 2 年仍会打印 2018 年,我期待 2019 年和 2020 年。当它显示 2018、2019 和 2020 时,我怎么可能动态打印年份。可能是我的代码中缺少一些迭代。

也可以随意批评我的代码,您也可以尽可能多地使用日历 API 或 Java 8 实用程序来建议更短的代码。

见下面的代码:

package calendarjava;

import java.util.Calendar;
import java.util.Scanner;
import java.util.GregorianCalendar;
import java.util.Locale;


public class CalendarJava {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();

Calendar cal = new GregorianCalendar();

int startDay;
int numberOfDays;
for (int i=0; i<36; i++){
cal.set(year, i, 1);
startDay = cal.get(Calendar.DAY_OF_WEEK);
numberOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.print(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US));
System.out.println( " " + year);
printMonth(numberOfDays,startDay);
System.out.println();
}
}

private static void printMonth(int numberOfDays, int startDay) {

int weekdayIndex = 0;
System.out.println("Su Mo Tu We Th Fr Sa");

for (int day = 1; day < startDay; day++) {
System.out.print(" ");
weekdayIndex++;
}

for (int day = 1; day <= numberOfDays; day++) {
System.out.printf("%1$2d", day);
weekdayIndex++;
if (weekdayIndex == 7) {
weekdayIndex = 0;
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Enter a year: 2018
January 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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

March 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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

January 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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

March 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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

January 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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 2018
Su Mo Tu We Th Fr Sa
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

最佳答案

现代Java

这是一个完全不同的观点,供您比较。此代码使用现代 Java 的功能,包括 java.time JSR 310 中定义的类, streams , 方便List factory方法,和enums .

所有这些代码都包含在一个 .java 文件中,用于定义我们的 CalendarMaker 类。请参阅 main 方法作为演示,了解如何使用此类。

该类有两个您通过构造函数注入(inject)的成员变量:end-of-line (newline)在生成的文本中使用的字符,以及我们 (a) 确定星期几的顺序,以及 (b) 本地化月份名称和星期几的名称。

我们使用 StringBuilder类通过调用其 append 方法来构建我们的文本。

尽可能使用特定的类型,使您的代码更加self-documenting ,确保有效值,并提供type-safety .所以我们从 Year 的列表开始对象,用于当前年份以及之前和之后的年份。

对于每一年,我们循环月份。每个月都表示为 YearMonth目的。我们通过调用 Month.getDisplayName 本地化月份的名称。然后我们通过首先本地化星期几的名称来本地化星期几列标题,然后截断以仅使用前两个字母。

DayOfWeek enum 提供现成的对象来表示一周中的每一天。

请注意,我们还本地化了星期几的顺序。在美国,星期日在大多数日历上开始一周。但在欧洲和其他地方,您通常会先看到星期一。我们的代码允许一周中的任何一天作为一周的开始,以防某些文化规范有其他选择。

A TemporalAdjusterTemporalAdjusters 中找到class 在我们的每月网格中确定开始日期的日期。然后我们每天增加,每周增加一次。 (可选)我们禁止显示目标月份之外的日期。

要为每一天的数字生成文本,请使用 DateTimeFormatter。使用 dd 格式模式用零填充单个数字。要用空格填充,请使用 ppd

更新:我用来自 LocalDate.datesUntil 的流替换了这段代码中的 for 循环.在里面我们使用 ternary operator抑制目标月份之外的日期。我并不是说这种重写一定更好;作为现代 Java 编程的一个例子,我只是想用流和 lambda 来展示漂亮的语法。

    // Rows (each week)
LocalDate localDate = yearMonth.atDay( 1 ).with( TemporalAdjusters.previousOrSame( firstDayOfWeek ) );
while ( ! localDate.isAfter( yearMonth.atEndOfMonth() ) ) // "Not after" is a shorter way of saying "is equal to or sooner than".
{
for ( int i = 0 ; i < 7 ; i++ )
{
// If we want to suppress the out-of-month dates that may exist in first and last rows.
if ( ! YearMonth.from( localDate ).equals( yearMonth ) )
{
sb.append( " " ); // Use 2 spaces rather than 2 digits of day-of-month number.
} else // Else the date is inside our target year-month.
{
sb.append( localDate.format( CalendarMaker.DAY_FORMATTER ) );
}
if ( i < 6 )
{
sb.append( " " ); // Pad with a SPACE between columns.
}
localDate = localDate.plusDays( 1 ); // Increment one day at a time.
}
sb.append( this.eol );
}

……变成了:

    // Rows (each week)
LocalDate localDate = yearMonth.atDay( 1 ).with( TemporalAdjusters.previousOrSame( firstDayOfWeek ) ); // Get the first date of the month, then move backwards in time to determine the first date that fits our calendar grid. May be the same as the first, or may be earlier date from the previous month.
while ( ! localDate.isAfter( yearMonth.atEndOfMonth() ) ) // "Not after" is a shorter way of saying "is equal to or sooner than".
{
String week =
localDate
.datesUntil( localDate.plusWeeks( 1 ) ) // Get a stream of dates via `LocalDate::datesUntil`. The ending date is exclusive (half-open).
.map( ld -> ( YearMonth.from( ld ).equals( yearMonth ) ? ld.format( CalendarMaker.DAY_FORMATTER ) : " " ) ) // Display the day-of-month number if within the target month, otherwise display a pair of SPACE characters.
.collect( Collectors.joining( " " ) ); // Separate columns with a SPACE in our calendar grid.
sb.append( week ).append( this.eol ); // Add this row of text for the week, and wrap to next line for next loop.
localDate = localDate.plusWeeks( 1 ); // Increment one week at a time to set up our next loop.
}

日历制作器.java

package work.basil.example;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class CalendarMaker
{
// Member variables.
private String eol;
private Locale locale;
static private DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern( "ppd" ); // Use `dd` to pad single-digits values with a leading zero. Use `ppd` to pad with a SPACE.

// Constructor

public CalendarMaker ( String eol , Locale locale )
{
this.eol = eol;
this.locale = locale;
}

private CharSequence generateYear ( final Year year )
{
// Year header.
StringBuilder sb = new StringBuilder();
sb.append( "|------ " + year + " ------|" ).append( this.eol ).append( this.eol );

// Each month.
for ( Month month : EnumSet.allOf( Month.class ) )
{
YearMonth ym = YearMonth.of( year.getValue() , month );
CharSequence monthCalendar = this.generateMonth( ym );
sb.append( monthCalendar );
}
return sb;
}

private CharSequence generateMonth ( final YearMonth yearMonth )
{
// Title
StringBuilder sb = new StringBuilder();
String monthName = yearMonth.getMonth().getDisplayName( TextStyle.FULL , this.locale );
sb.append( yearMonth.getYear() ).append( " " ).append( monthName ).append( this.eol );

// Column headers.
DayOfWeek firstDayOfWeek = WeekFields.of( this.locale ).getFirstDayOfWeek();
List < DayOfWeek > dows =
IntStream
.range( 0 , 7 )
.mapToObj( firstDayOfWeek :: plus )
.collect( Collectors.toList() );
String columnHeaders =
dows
.stream()
.map( dayOfWeek -> dayOfWeek.getDisplayName( TextStyle.SHORT_STANDALONE , this.locale ).substring( 0 , 2 ) )
.collect( Collectors.joining( " " ) );
sb.append( columnHeaders ).append( this.eol );

// Rows (each week)
LocalDate localDate = yearMonth.atDay( 1 ).with( TemporalAdjusters.previousOrSame( firstDayOfWeek ) ); // Get the first date of the month, then move backwards in time to determine the first date that fits our calendar grid. May be the same as the first, or may be earlier date from the previous month.
while ( ! localDate.isAfter( yearMonth.atEndOfMonth() ) ) // "Not after" is a shorter way of saying "is equal to or sooner than".
{
String week =
localDate
.datesUntil( localDate.plusWeeks( 1 ) ) // Get a stream of dates via `LocalDate::datesUntil`. The ending date is exclusive (half-open).
.map( ld -> ( YearMonth.from( ld ).equals( yearMonth ) ? ld.format( CalendarMaker.DAY_FORMATTER ) : " " ) ) // Display the day-of-month number if within the target month, otherwise display a pair of SPACE characters.
.collect( Collectors.joining( " " ) ); // Separate columns with a SPACE in our calendar grid.
sb.append( week ).append( this.eol ); // Add this row of text for the week, and wrap to next line for next loop.
localDate = localDate.plusWeeks( 1 ); // Increment one week at a time to set up our next loop.
}

// Footer (for the month)
sb.append( this.eol ); // Put a blank line after every month.
return sb;
}

// Demonstrate this class with a psvm method.
public static void main ( String[] args )
{

CalendarMaker calendarMaker = new CalendarMaker( "\n" , Locale.CANADA_FRENCH );

// Demonstrate 3 years: previous year, current, and next year.
Year currentYear = Year.now( ZoneId.of( "America/Boise" ) );
List < Year > years = List.of( currentYear.minusYears( 1 ) , currentYear , currentYear.plusYears( 1 ) );
for ( Year year : years )
{
CharSequence calendar = calendarMaker.generateYear( year );
System.out.println( "" );
System.out.println( calendar );
}
}

}

运行时。

|------  2018  ------|

2018 janvier
di lu ma me je ve sa
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

2018 février
di lu ma me je ve sa
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

Locale.CANADA_FRENCH 切换语言环境至 Locale.FRANCE看看我们如何保留法语,但将文化规范从北美转变为欧洲,以星期一 (lundi) 而不是星期日 (dimanche) 开始一周。

关于java - 使用 Java 打印 12 月年和接下来的 2 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56213381/

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