我最近在一個程序中詢問用戶年齡:年,月,日。使用開關語句的效率,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