2013-02-22 23 views
1

我有這個代碼來確定基於用戶輸入的里氏計值的地震時的抵扣和支付。這是我的代碼目前的樣子。哨兵控制循環以及如何打印出多個字符串。初學java編程

import java.util.Scanner; 

public class RichterScaleDamage 
{ 
    public static void main(String[] args) 
    { 
    Scanner userInput = new Scanner(System.in); 
    String name = ""; 
    char answer = 'Y'; 
    double homeValue = 0; 
    double richterScale = 0; 
    double payout = 0; 
    double deductible = 0; 
    String coverage = ""; 

    System.out.printf("\nPlease enter your name: "); 
    name = userInput.nextLine(); 


    while(Character.toUpperCase(answer) == 'Y') 
    { 
    System.out.printf("\nPlease enter the insured value of your home: "); 
    homeValue = userInput.nextDouble(); 
    userInput.nextLine(); 

    System.out.printf("\nRichter Scale Description of Effect" 
       +"\n  8.0  Most structures fall" 
       +"\n  7.0  Many buildings destroyed" 
       +"\n  6.0  Many buildings considerably damaged, some collapse" 
       +"\n  4.5  Damage to poorly constructed buildings" 
       +"\n  3.5  Felt by many people, no destruction" 
       +"\n  0  Generally not felt by people\n\n"); 

System.out.printf("\nPlease enter the Richter scale value for the earthquake: "); 
richterScale = userInput.nextDouble(); 
userInput.nextLine(); 

if(richterScale < 0) 
{ 
    System.out.printf("\nInvalid! Cannot enter negative values"); 
} 
System.out.printf("\n\nEnter \'Y\' to continue with another calculation or \'N\' to exit: "); 
answer = userInput.nextLine().charAt(0); 


    } 


    if(richterScale >= 8) 
    { 
    String message = "Most structures fall"; 
    payout = homeValue * .85; 
    deductible = homeValue * .15; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 

    System.out.printf("%s", coverage); 
    } 
    if(richterScale < 8 && richterScale >= 7) 
    { 
    String message = "Many buildings destroyed"; 
    payout = homeValue * .75; 
    deductible = homeValue * .25; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 

    System.out.printf("%s", coverage); 
    } 
    if(richterScale < 7 && richterScale >= 6) 
    { 
    String message = "Damage to poorly constructed buildings"; 
    payout = homeValue * .75; 
    deductible = homeValue * .25; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
    System.out.printf("%s", coverage); 
    } 

    if(richterScale < 6 && richterScale >= 4.5) 
    { 
    String message = "Many buildings considerably damaged, some collapse"; 
    payout = homeValue * .65; 
    deductible = homeValue * .35; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
    System.out.printf("%s", coverage); 
    } 

    if(richterScale < 4.5 && richterScale >= 3.5) 
    { 
    String message = "Felt by many people, no destruction"; 
    payout = homeValue * .55; 
    deductible = homeValue * .45; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
    System.out.printf("%s", coverage); 
    } 

    if(richterScale < 3.5 && richterScale >= 0) 
    { 
    String message = "Generally not felt by people"; 
    payout = homeValue * .0; 
    deductible = homeValue * .25; 
    coverage += String.format("\n%-50s %6s$%,20.2f" 
         +"\nDeductable%47s %,20.2f" 
         +"\n%46sTOTAL %4s $%,20.2f\n", 
          message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
    System.out.printf("%s", coverage); 
    } 

    } 
} 

我還有一些代碼,我還沒有填寫,我很抱歉,如果這太多了,我是非常新的網站。如果用戶輸入Y,他們將輸入另一個家庭的另一個值,以及家庭所受的損害等級。現在,我的輸出只顯示輸入的最新值,而不是預先顯示的所有其他值。我已經閱讀了我的書,並且對於如何讓我的輸出顯示的不僅僅是最後一個用戶條目感到困惑。這是一個輸出的例子!

 
Please enter your name: Name 

Please enter the insured value of your home: 1000000 

Richter Scale Description of Effect 
     8.0  Most structures fall 
     7.0  Many buildings destroyed 
     6.0  Many buildings considerably damaged, some collapse 
     4.5  Damage to poorly constructed buildings 
     3.5  Felt by many people, no destruction 
     0  Generally not felt by people 


Please enter the Richter scale value for the earthquake: 5 


Enter 'Y' to continue with another calculation or 'N' to exit: y 

Please enter the insured value of your home: 349999 

Richter Scale Description of Effect 
     8.0  Most structures fall 
     7.0  Many buildings destroyed 
     6.0  Many buildings considerably damaged, some collapse 
     4.5  Damage to poorly constructed buildings 
     3.5  Felt by many people, no destruction 
     0  Generally not felt by people 


Please enter the Richter scale value for the earthquake: 6 


Enter 'Y' to continue with another calculation or 'N' to exit: n 

Damage to poorly constructed buildings     $   262,499.25 
Deductable               87,499.75 
               TOTAL  $   349,999.00 

我想讓最後一部分顯示出儘可能多的用戶輸入信息。對不起,如果我不知道使這個更有意義的必要術語。我參加了我的第一門編程課程,而且只進行了一個月的學習。任何幫助解決這個問題將不勝感激。

回答

2

我想你想移動右括號while循環,直到你的條件後,只打印出結果,循環終止後:

import java.util.Scanner; 

public class RichterScaleDamage { 

    public static void main(String[] args) { 
     Scanner userInput = new Scanner(System.in); 
     String name = ""; 
     char answer = 'Y'; 
     double homeValue = 0; 
     double richterScale = 0; 
     double payout = 0; 
     double deductible = 0; 
     String coverage = ""; 

     System.out.printf("\nPlease enter your name: "); 
     name = userInput.nextLine(); 


     while (Character.toUpperCase(answer) == 'Y') { 
      System.out.printf("\nPlease enter the insured value of your home: "); 
      homeValue = userInput.nextDouble(); 
      userInput.nextLine(); 

      System.out.printf("\nRichter Scale Description of Effect" 
        + "\n  8.0  Most structures fall" 
        + "\n  7.0  Many buildings destroyed" 
        + "\n  6.0  Many buildings considerably damaged, some collapse" 
        + "\n  4.5  Damage to poorly constructed buildings" 
        + "\n  3.5  Felt by many people, no destruction" 
        + "\n  0  Generally not felt by people\n\n"); 

      System.out.printf("\nPlease enter the Richter scale value for the earthquake: "); 
      richterScale = userInput.nextDouble(); 
      userInput.nextLine(); 

      if (richterScale < 0) { 
       System.out.printf("\nInvalid! Cannot enter negative values"); 
      } 
      System.out.printf("\n\nEnter \'Y\' to continue with another calculation or \'N\' to exit: "); 
      answer = userInput.nextLine().charAt(0); 




      if (richterScale >= 8) { 
       String message = "Most structures fall"; 
       payout = homeValue * .85; 
       deductible = homeValue * .15; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 

      } 
      if (richterScale < 8 && richterScale >= 7) { 
       String message = "Many buildings destroyed"; 
       payout = homeValue * .75; 
       deductible = homeValue * .25; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 

      } 
      if (richterScale < 7 && richterScale >= 6) { 
       String message = "Damage to poorly constructed buildings"; 
       payout = homeValue * .75; 
       deductible = homeValue * .25; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
      } 

      if (richterScale < 6 && richterScale >= 4.5) { 
       String message = "Many buildings considerably damaged, some collapse"; 
       payout = homeValue * .65; 
       deductible = homeValue * .35; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
      } 

      if (richterScale < 4.5 && richterScale >= 3.5) { 
       String message = "Felt by many people, no destruction"; 
       payout = homeValue * .55; 
       deductible = homeValue * .45; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
      } 

      if (richterScale < 3.5 && richterScale >= 0) { 
       String message = "Generally not felt by people"; 
       payout = homeValue * .0; 
       deductible = homeValue * .25; 
       coverage += String.format("\n%-50s %6s$%,20.2f" 
         + "\nDeductable%47s %,20.2f" 
         + "\n%46sTOTAL %4s $%,20.2f\n", 
         message, " ", payout, " ", deductible, " ", " ", payout + deductible); 
      } 

     } 
     System.out.printf("%s", coverage); 
    } 
} 
+0

謝謝你,這其實得到我想要的結果!我猜想通過等待退出循環,一旦用戶選擇退出循環,將打印消息。雖然我不是100%確定這背後的邏輯是什麼,但它是有效的。 – ShiftyMcGrifty 2013-02-22 04:50:13