2015-09-28 61 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