我最近参与了一个程序,询问用户的年龄:年,月和日。
收到该输入后,必须计算并打印
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.
答案 0 :(得分:2)
要正确计算用户活着的天数,您应首先根据提供的数据和今天的日期计算出生日期。例如:
之后,您可以以秒为单位计算差异。 Java API中有现成的类和方法来执行这些步骤。最简单的方法是使用Java 8 Time API:
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class AgeInSeconds {
public static void main(String[] args) {
try (Scanner kbd = new Scanner(System.in)) {
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();
Period period = Period.of(years, months, days);
LocalDateTime now = LocalDateTime.now();
LocalDateTime birthDate = now.minus(period);
long seconds = birthDate.until(now, ChronoUnit.SECONDS);
long avgLifeSecs = 2500000000l;
long timeRemaining = avgLifeSecs - seconds;
System.out.printf("You have been alive for %,d seconds.\n", seconds);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
}
}
}
我这里没有解决统计问题。要计算估计的剩余寿命(假设我是普通人),您应该平均死亡人数超过我的生命周期。
答案 1 :(得分:0)
您的整数daysInMonth
将通过switch语句传输。因此,你可以在之后使用重复代码。。
根据经验:当你有重复的代码时,将它放在它自己的方法中,或者整合代码,这样你只需要在一个地方调用它。
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;
break;
case 2:
daysInMonth = 59;
break;
case 3:
daysInMonth = 90;
break;
case 4:
daysInMonth = 120;
break;
case 5:
daysInMonth = 151;
break;
case 6:
daysInMonth = 181;
break;
case 7:
daysInMonth = 212;
break;
case 8:
daysInMonth = 243;
break;
case 9:
daysInMonth = 273;
break;
case 10:
daysInMonth = 304;
break;
case 11:
daysInMonth = 334;
break;
case 12:
daysInMonth = 365;
break;
default:
daysInMonth = 0;
}
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);
kbd.close();
}
}
以上是代码的“快速而肮脏”的解决方案,因此您可以看到如何通过switch语句传输变量。
更好的做法是使用模数运算符检查它是奇数还是偶数,如果它不是第二个月,则给出正确的值。
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;
/** predifine with 0 so we always have a value **/
daysInMonth = 0;
/** Our months here. Please consider using calendar **/
int[] legaldays = {31,28,31,30,31,30,31,31,30,31,30,31};
/** Looping through all months **/
for(i=0;i<legaldays.length;i++) {
/** check if we didn't pass our max limit **/
if(i+1 > daysInMonth) {
break;
}
/** add the days to our tally **/
daysInMonth += legaldays[i];
}
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);
kbd.close();
}
}
请参阅评论以了解我是如何改进的。通过循环,您可以免去难以对数月的值进行硬编码,并为您提供一定的灵活性。 为了更可靠的日计数而不是硬编码,我建议您查看Number of days in a month of a particular year,这样您就可以灵活处理闰年等问题。
尽可能尝试不硬编码无形或动态数据值,但要尽量准确地扣除它们。众所周知,日期难以保持秩序。