我最近在一个程序中询问用户年龄:年,月,日。使用开关语句的效率,Java
接收输入之后,它必须计算和打印
a)年龄在秒(totalAgeInSecs)和
B)秒的量可活。 b将基于平均寿命秒数(avgLifeSpan = 250000000000l. So secondsLeft = avgLifeSpan - totalAgeInSecs)
。
无论如何,为了简单起见,我能够使用(switch)语句来工作,而不必编写一堆if/else语句,但是我觉得在这样做的时候,我最终编写了重复行,我希望能够不必重复计算或打印报表。
我知道有些类和数组可以与循环结合使用,但为了简单和逻辑理解,我没有使用它们来理解这个项目的无鞍骨骼和逻辑在“英语”中。哈哈。
无论如何,检查下面的代码,让我知道你如何简化重复线或更好的方法来解决这个问题的想法。谢谢。
import java.util.*;
public class AgeInSeconds {
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args) {
int totalNumDays, daysInMonth, daysToHours;
int yrsToDays,minsInHr, secsInMin;
long timeRemaining, avgLifeSecs;
System.out.println("Enter your age in years months and days: ");
System.out.print("Years: ");
int years = kbd.nextInt();
System.out.print("Months: ");
int months = kbd.nextInt();
System.out.print("Days: ");
int days = kbd.nextInt();
yrsToDays = years * 365;
avgLifeSecs = 2500000000l;
switch (months){
case 1:
daysInMonth = 31;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 2:
daysInMonth = 59;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 3:
daysInMonth = 90;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 4:
daysInMonth = 120;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 5:
daysInMonth = 151;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 6:
daysInMonth = 181;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 7:
daysInMonth = 212;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 8:
daysInMonth = 243;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 9:
daysInMonth = 273;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 10:
daysInMonth = 304;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 11:
daysInMonth = 334;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 12:
daysInMonth = 365;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
default:
}
kbd.close();
}
}
这是当输出:年= 24个月= 5天= 8
Enter your age in years months and days:
Years: 24
Months: 5
Days: 8
You have been alive for 770,601,600 seconds.
The average human life is 2,500,000,000 seconds.
You have 1,729,398,400 seconds.
这是一个应该调用方法并将整个代码放入其中的情况。 – SomeJavaGuy
闰年呢?顺便说一下,从统计的角度来看,程序的结果是不正确的。如果我100岁,该怎么办?我有负秒数?那没有意义。 –
这确实重复了很多代码。另外还有一个叫闰年的东西。 – laune