我是一个非常业余的编码员,他似乎无法解决从用户输入的整数中获取一周中的某一天(例如周日、周一等)的问题。我只是想给用户一条消息,其中列出了(mm/dd/yyyy,dayOfWeek),但是无论我在里面输入什么,我都会得到星期三作为一周中的某一天的答案,而不是改变星期几提示框。我只是需要一个新的方向。我的代码有任何错误或我没有看到的途径吗?任何帮助将不胜感激。
public static void main(String [] args)
{
Scanner user_input = new Scanner (System.in);
String returnValue = "";
int month2=0;
int day2=0;
int year2=0;
GregorianCalendar userbirthday = new GregorianCalendar(year2, month2, day2);
int userweekday=userbirthday.get(Calendar.DAY_OF_WEEK);
String usermonth;
System.out.print ("What month is your birthday?");
usermonth = user_input.next();
String userday;
System.out.print ("What day is your birthday?");
userday = user_input.next();
String useryear;
System.out.print ("What year was your birth?");
useryear = user_input.next();
year2 = Integer.parseInt(useryear);
month2 = Integer.parseInt(usermonth);
day2 = Integer.parseInt(userday);
String dayOfTheWeek = "";
if(month2 == 0){
System.out.println("That's not a valid birthday! Check your month.");
System.exit(0);
} else if (month2>=13){
System.out.println("That's not a valid birthday! Check your month.");
System.exit(0);
}
if(day2 == 0){
System.out.println("That's not a valid birthday! Check your day.");
System.exit(0);
} else if (day2>=32){
System.out.println("That's not a valid birthday! Check your day.");
System.exit(0);
}
if(userweekday == 2){
dayOfTheWeek= "Mon";
} else if (userweekday==3){
dayOfTheWeek = "Tue";
} else if (userweekday==4){
dayOfTheWeek = "Wed";
} else if (userweekday==5){
dayOfTheWeek = "Thu";
} else if (userweekday==6){
dayOfTheWeek = "Fri";
} else if (userweekday==7){
dayOfTheWeek = "Sat";
} else if (userweekday==1){
dayOfTheWeek = "Sun";
}
String birthdate = month2 + "/" + day2 + "/" + year2 + "," + dayOfTheWeek;
System.out.println ("Your birthday was" + " " + birthdate);
}
最佳答案
您做事的顺序错误。正如您在问题中的代码所示,发生了以下情况:
- 您正在将
month2
、day2
和year2
初始化为 0。 - 您正在从那些值为 0 的变量创建您的
GregorianCalendar
对象。这将为您提供公元前 2 年 12 月 31 日星期三(没有年份 0,因此您将设置为 1 月 0 日)公元前 1 年,等于公元前 2 年 12 月 31 日——令人困惑,但对于 GregorianCalendar 来说这是事实)。然后将星期几放入userweekday
(始终等于Calendar.WEDNESDAY
)。 - 最后,在
year2 = Integer.parseInt(useryear);
等行中,您将用户输入分配给变量。这不会影响您已创建的GregorianCalendar
对象,也不会影响已从该GregorianCalendar
获取的星期几。
因此星期三将始终被打印。
顺便说一句,GregorianCalendar
类早已过时并且存在设计问题。除其他问题外,它通常需要冗长的代码,而且也容易出错。几个月的数字是出乎意料的。相反,我建议您使用 java.time 中的 LocalDate
,这是现代 Java 日期和时间 API。
LocalDate // Represent a date-only value, without time-of-day, without time zone.
.of( y , m , d ) // Specify year-month-day, 1-12 for January-December.
.getDayOfWeek() // Extract a `DayOfWeek` enum object representing one of seven days of the week.
.getDisplayName( // Automatically localize the text of the name of the day of the week.
TextStyle.SHORT , // Specify how long or abbreviated.
Locale.US // Locale determines the human language and cultural norms used in localizing.
)
链接: Oracle tutorial: Date Time解释如何使用java.time
。
关于java - 公历 : Getting a user's (day_of_the_week) from their own birthday input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661046/