gpt4 book ai didi

Java 基本日历打印

转载 作者:行者123 更新时间:2023-11-30 07:50:13 25 4
gpt4 key购买 nike

我正在为我的计算机科学 1 类(class)做作业,但被困在某个部分。该代码的编写是为了让用户输入年份,然后输入星期几(使用 0 到 6)来启动日历。我将脱离我的导师框架来编写代码。我当前的问题是我无法获得 7 天后打印新行的代码。提前致谢!

import java.util.Scanner;
public class Test {

/**
* Determines if an input is a leap year
*
* @param year year in question
* @return true if a leap year
*/
public static boolean isLeapYear(int year) {
if((year % 4 == 0) || (year % 100 == 0)) return true;
return false;
}

/**
* Outputs a month to the console
*
* @param month title
* @param startDay 0=Sunday ... 6=Saturday
* @param numDays number of days in the month
* @return day of the week of the last day of the month
*/
public static int printMonth(String month, int startDay, int numDays){
System.out.println(month);
int dayOfWeek = 10;

for(int i = 0; i < startDay; i++){
System.out.print(" ");
}

for(int i = 1; i < numDays; i++){
System.out.printf("%2d ", i);
if((i + startDay) % 7 == 0);{
System.out.print("");
}
}
//Your code goes here
System.out.println("Print: " + month);
System.out.println("");

return dayOfWeek;
}

/**
* Program execution point:
* input year, day of the week (0-6) of january 1
* output calendar for that year
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
final Scanner input = new Scanner(System.in);

System.out.print("Enter the year: ");
final int year = input.nextInt();

System.out.print("Enter the day of the week of January 1st (0=Sunday, 1=Monday, ... 6=Saturday): ");
final int firstDay = input.nextInt();

if (year<=0) {
System.out.println("The year must be positive!");
System.exit(0);
}

if (firstDay<0 || firstDay>6) {
System.out.println("The day of January 1st must be between 0 and 6!");
System.exit(0);
}

final int numFebDays;
if (isLeapYear(year)) {
numFebDays = 29;
} else {
numFebDays = 28;
}

int lastDayOfWeek;
lastDayOfWeek = printMonth("January", firstDay, 31);
lastDayOfWeek = printMonth("February", lastDayOfWeek, numFebDays);
lastDayOfWeek = printMonth("March", lastDayOfWeek, 31);
lastDayOfWeek = printMonth("April", lastDayOfWeek, 30);
lastDayOfWeek = printMonth("May", lastDayOfWeek, 31);
lastDayOfWeek = printMonth("June", lastDayOfWeek, 30);
lastDayOfWeek = printMonth("July", lastDayOfWeek, 31);
lastDayOfWeek = printMonth("August", lastDayOfWeek, 31);
lastDayOfWeek = printMonth("September", lastDayOfWeek, 30);
lastDayOfWeek = printMonth("October", lastDayOfWeek, 31);
lastDayOfWeek = printMonth("November", lastDayOfWeek, 30);
lastDayOfWeek = printMonth("December", lastDayOfWeek, 31);

}
}

最佳答案

这个程序有几个问题。注释中提到了其中两个,将 System.out.print("") 更改为 System.out.println(""),并删除 ;在 for 语句的末尾。

此外,您不会更改 dayOfWeek 的值。它总是返回 10,这就是为什么它打印到最右边的地方。应该是

int dayOfWeek = (startDay + numDays) % 7;

我还注意到计算闰年的公式有点不正确。一年是闰年,如果它能被 4 整除,除非它也能被 100 整除,除非它是闰年,如果它能被 400 整除。这是一个有效的函数:

public static boolean isLeapYear(int year) {
return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ;
}

我看到的另一个错误是没有打印该月的最后一天。通过更改 for 循环的这一部分可以解决这个问题:i <= numDays;

关于Java 基本日历打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402368/

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