2015-09-28 43 views
0

我最近在一个程序中询问用户年龄:年,月,日。使用开关语句的效率,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. 
+2

这是一个应该调用方法并将整个代码放入其中的情况。 – SomeJavaGuy

+1

闰年呢?顺便说一下,从统计的角度来看,程序的结果是不正确的。如果我100岁,该怎么办?我有负秒数?那没有意义。 –

+0

这确实重复了很多代码。另外还有一个叫闰年的东西。 – laune

回答

0

你的整数daysInMonth将转移过去switch语句。因此,您可以在 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,这样你就可以灵活地用闰年等等。

尽可能尝试不对无形或动态数据值进行硬编码,但要尽可能准确地扣除它们。日期很难保持秩序。

+0

曾经使用过一个数组来存储基于1..12(或0..11)的值吗? – laune

+0

我认为,但他无效输入的情况。再加上我仍在编辑一些最佳做法。 – Tschallacka

+0

还要考虑OP提出的方法是无稽之谈。如果我今天是x年和5个月,那么第二个月肯定没有28天。 – laune

2

要正确计算用户存活的天数,您应该首先根据提供的数据和今天的日期计算他的出生日期。例如:

  • 用户1个月大,当前日期为2015年9月28日,因此用户出生于2015年8月28日,他已经31天。
  • 用户为1个月大,当前日期为2015年3月2日,因此用户出生于2015年2月2日,他的年龄为28岁。

之后,您可以计算以秒为单位的差异。 Java API中有准备好的类和方法来完成这些步骤。最简单的是使用Java 8时间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); 
     } 
    } 
} 

我没有解决这里的统计问题。要计算估计剩余寿命(假设我是一般人),您应该平均比我年长的人寿命。

+0

我想你可能会超过OP的头。我认为他在第一章或第二章中学习如何编码;-)但答案很好,+1 – Tschallacka