gpt4 book ai didi

java - 案例没有落到正确的数字/程序没有正确读取时钟

转载 作者:行者123 更新时间:2023-12-04 05:23:27 26 4
gpt4 key购买 nike

我正在编写关于 switch/cases 的教程,我相信我已经将程序复制到了角色,但它返回了不正确的月份。有人发现我的错误吗?谢谢。

导入 java.util.*;
类时钟对话{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//get current time and date
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);

//display greeting
if (hour < 12)
System.out.println("Good moring.\n");
else if (hour < 17)
System.out.println("Good afternoon.\n");
else
System.out.println("Good evening.\n");

//begin time message by showing the minutes
System.out.print("It's");
if (minute != 0) {
System.out.print(" " + minute + " ");
System.out.print( (minute != 1) ? "minutes" : "minute");
System.out.print(" past");
}

//Display the hour
System.out.print(" ");
System.out.print( (hour > 12) ? (hour - 12) : hour );
System.out.print(" o'clock on ");

//display the name of the month
switch (month) {
case 1:
System.out.print("January");
break;
case 2:
System.out.print("Febuary");
break;
case 3:
System.out.print("March");
break;
case 4:
System.out.print("April");
break;
case 5:
System.out.print("May");
break;
case 6:
System.out.print("June");
break;
case 7:
System.out.print("July");
break;
case 8:
System.out.print("August");
break;
case 9:
System.out.print("September");
break;
case 10:
System.out.print("October");
break;
case 11:
System.out.print("November");
break;
case 12:
System.out.print("December");

}


//display the date and year
System.out.println(" " + day + ", " + year + ".");



}

}

最佳答案

Calendar.MONTH从零开始。这可能是原因。您需要添加 case 0:
示例:

  case 0:
System.out.print("January");
break;

按日历 javadoc

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

关于java - 案例没有落到正确的数字/程序没有正确读取时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13463568/

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