我正在编写一个程序,其中包含用户出生信息,年,月,日,小时,分钟。该计划如下:
使用
getRangedInt
方法输入年份(1965-2000),月份 (1-12),一天*,小时(1-24),一个人出生的分钟(1-59)。注意:使用switch()条件选择器结构来限制用户 他们出生的月份的正确天数 例如,他们出生于2月[1-29],10月[1-31]。提示:有 这里只有少数几个团体不是12个不同的团体!
我对这一天非常困惑。我已经设法让其他一切工作,除了明白我今天应该做的事情。
我的代码:
public static void main(String[] args)
{
int year, month, day, hour, minutes;
String msg="";
boolean done = true;
Scanner in = new Scanner(System.in);
while(done)
{
year = SafeInput.getIntInRange(in, "Enter the year you were born: ", 1965, 2000);
month = SafeInput.getIntInRange(in, "Enter your month of birth: ", 1, 12);
day = SafeInput.getIntInRange(in, "Enter the day you were born: ", day, day);
hour = SafeInput.getIntInRange(in, "Enter the hour you were born in: ", 1, 24);
minutes = SafeInput.getIntInRange(in, "Enter the minutes you were born: ", 1, 59);
System.out.println("You were born: " + year + " , " + msg + " , " + hour + " hr. " + minutes + " mins. ");
done = SafeInput.getYNConfirm(in, "Would you like to play again?");
}
}
getIntInRange
只是做一个基本输入来获取范围内的数字。
我很困惑,因为我不知道如何做当天的范围,因为还应该使用开关。
答案 0 :(得分:1)
这是一个非常奇怪的要求。
看起来你的要求看起来像这样:
int numberOfDays;
switch (month) {
case 1: // $FALL-THROUGH
case 3: // $FALL-THROUGH
case 5: // $FALL-THROUGH
case 7: // $FALL-THROUGH
case 9: // $FALL-THROUGH
case 11:
numberOfDays = 31;
break;
case 4: // $FALL-THROUGH
case 6: // $FALL-THROUGH
case 8: // $FALL-THROUGH
case 10: // $FALL-THROUGH
case 12:
numberOfDays = 30;
break;
case 2:
numberOfDays = (0 == year / 4) ? 29 : 28;
break;
default:
throw new IllegalArgumentException("month not 1-12");
} // switch (too long for my liking)
然后
day = SafeInput.getIntInRange(in, "Enter the day you were born: ", 1, numberOfDays);
这利用了开关中可用的直通构造,因此如果你满足条件,你将执行直到返回语句(或中断/抛出)。
请记住,另一种选择是:
private static final Set<Integer> MONTHS_30_DAYS = Sets.newTreeSet(4, 6, 8, 10, 12);
private static final Set<Integer> MONTHS_31_DAYS = Sets.newTreeSet(1, 3, 5, 7, 9, 11);
public static int numberOfDays(int month, int year) {
if (MONTHS_30_DAYS.contains(month)) {
return 30;
} else if (MONTHS_31_DAYS.contains(month)) {
return 31;
} else {
return (0 == year / 4) ? 29 : 28;
}
}
答案 1 :(得分:0)
此处您的一天取决于输入的月份。首先,您要将用户输入的月份视为您现在正在执行的整数,并继续在交换机中使用该数字:
int month = 0;
int numberOfDays;
switch (month) {
case 1: numberOfDays = DaysInJanuary;
break;
case 2: numberOfDays = DaysInFebruary;
break;
case 3: numberOfDays = DaysInMarch;
break;
case 4: numberOfDays = DaysInApril;
break;
case 5: numberOfDays = DaysInMay;
break;
case 6: numberOfDays = DaysInJune;
break;
case 7: numberOfDays = DaysInJuly;
break;
case 8: numberOfDays = DaysInAugust;
break;
case 9: numberOfDays = DaysInSeptember;
break;
case 10: numberOfDays = DaysInOctober;
break;
case 11: numberOfDays = DaysInNovember;
break;
case 12: numberOfDays = DaysInDecember;
break;
default: throw new IllegalArgumentException("Not a valid month");
break;
}
然后,您可以继续为最大日期范围设置变量。
day = SafeInput.getIntInRange(in, "Enter the day you were born: ", 1, numberOfDays);
希望这会有所帮助。
答案 2 :(得分:0)
如果您需要使用switch语句,则可以将月数除以其中的天数。将有3组。您可以按如下方式使用switch语句:
switch (month) {
case 1:
case 3:
case 5:
return 31;
case 2: {
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return 29;
else
return 28;
}
case 4:
case 6:
return 30;
default:
throw new IllegalArgumentException("some error text");
}
示例中的switch语句未满,您应该为每个月编写一个case语句。基本上你将返回当天的较高值。
答案 3 :(得分:0)
您的day
范围取决于用户输入的上一个month
号码,因此您必须检查该号码才能知道要显示的日期范围。提示告诉您的是,您每个月不需要不同的有效天数范围,因为有些月份共享相同的范围。以下是一个月内的天数:
January 31
February 28/29
March 31
April 30
May 31
June 30
July 31
August 31
September 30
October 31
November 30
December 31
正如您所看到的,只有3个小组,并且分配要求您使用 fallthrough - 当您不在break
中switch
时< / p>
int upperDaysLimit;
switch(month) {
case 1:
case 3:
case 5:
//...
case 12: upperDaysLimit = 31;
break;
case 4:
case 6:
// and so on
}
现在您可以在日期查询中使用该范围限制:
day = SafeInput.getIntInRange(in, "Enter the day you were born: ", 1, upperDaysLimit);