2015-10-20 61 views
1

嘗試捕捉我必須做出一個計算器,將While循環:負輸入檢查和添加字符串

  1. 用正數輸入,顯示輸出運行程序,如果用戶想循環問。

  2. 使用字符串輸入(兩個而不是2)運行程序,使用try和catch來顯示一條消息,告訴用戶錯誤,並詢問用戶是否想要循環。

  3. 使用負數輸入運行程序,顯示錯誤消息而不顯示輸出,並詢問用戶是否要循環。

當我試圖捕捉我得到variable not have been initializedtry catch後的變量。

try { 
    System.out.print("Enter loan amount: "); 
    loanNum = Double.parseDouble(in.nextLine()); 

    System.out.print("Enter rate: "); 
    rateNum = Double.parseDouble(in.nextLine()); 

    System.out.print("Enter number years: "); 
    yearNum = Double.parseDouble(in.nextLine()); 
} 
catch(NumberFormatException e) { 
    System.out.println ("You must enter positive numeric data!"); 
} 

沒有try catch我想不出該怎麼辦2號,而不使用System.exit(0);不顯示輸出。這終止了用戶能夠輸入到循環。

我不知道如何解決我的while循環。當我運行程序時,它只會運行一次,然後循環一次。之後,它會自動循環,而不提示和吐出像一行行:

Would you like to calculate again (y/n): 
Enter loan amount: 

代碼:

package whileloopyn; 

import java.util.Scanner; 

public class Whileloopyn { 

public static void main(String[] args) { 

    Scanner in = new Scanner(System.in); 

    //Declare Variables 
    double loanNum; 
    double rateNum; 
    double trateNum; 
    double yearNum; 
    double monthNum; 
    double equNum; 
    double outputNum; 

    //Prompt for input and try catch for exception error (rate inputed as two) 

    System.out.print("Enter loan amount: "); 
    loanNum = Double.parseDouble(in.nextLine()); 

    System.out.print("Enter rate: "); 
    rateNum = Double.parseDouble(in.nextLine()); 

    System.out.print("Enter number years: "); 
    yearNum = Double.parseDouble(in.nextLine()); 


    //Postive input data check (negative then ask again) 
    if(loanNum <= 0 || rateNum <=0 || yearNum <=0) 
    { 
     System.out.println("You must enter positive numeric data! "); 
     System.exit(0); 
    } 


    //Convert years to months 
    monthNum = yearNum * 12; 

    //Convert rate to percent and monthly 
    trateNum= (rateNum/100) /12; 

    //Complete numerator 
    equNum = Math.pow((1+ trateNum), monthNum); 

    //Plug equNum into formula 
    outputNum = trateNum * loanNum * (equNum/(equNum - 1)); 

    //output answer 
    System.out.printf("The monthly payment is: $ %.2f%n", outputNum); 

    //prompt user y n to run program again 
    System.out.println("Would you like to calculate again (y/n): "); 
    String loop = in.nextLine(); 

    //while loop with (input 

    boolean isContinuing = true; 

    while (isContinuing) { 
     while(loop.equals("y")){ 
     System.out.print("Enter loan amount: "); 
     loanNum = in.nextDouble(); 

     System.out.print("Enter rate: "); 
     rateNum = in.nextDouble(); 

     System.out.print("Enter number years: "); 
     yearNum = in.nextDouble(); 

     //Postive input data check (negative then loop again) 
     if(loanNum <= 0 || rateNum <=0 || yearNum <=0) 
     { 
      System.out.println("You must enter positive numeric data! "); 
      System.exit(0); 
     } 


     //Convert years to months 
     monthNum = yearNum * 12; 

     //Convert rate to percent and monthly 
     trateNum= (rateNum/100) /12; 

     //Complete numerator 
     equNum = Math.pow((1+ trateNum), monthNum); 

     //Plug equNum into formula 
     outputNum = trateNum * loanNum * (equNum/(equNum - 1)); 

     //output answer 
     System.out.printf("The monthly payment is: $ %.2f%n", outputNum); 
     //ask user if you want to use program again 
     System.out.printf("Would you like to calculate again (y/n): "); 

     if(loop.equals("n")) 
     { 
      System.exit(0); 
     } 
    } 
    } 

} 

} 

有人建議(input.equals(String))是我應該如何比較的字符串while循環並使用in.nextLine();

+0

你真正需要得到輸入,如果你問用戶,而不是直接進入if條件。你也需要通過調用'nextLine'來使用任何'nextFoo'(不包括'nextLine')方法來捕獲回車符。對於此檢查的參考[this](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – SomeJavaGuy

回答

1

如果用戶按下Y你要繼續這樣的循環:

 System.out.printf("Would you like to calculate again (y/n): "); 
     Scanner s = new Scanner(System.in); 
     if(s.nextLine().equalsIgnoreCase("y")) { 
      continue; 
     } else { 
      break; 
     } 

,取而代之的while(loop.equals("y")){你應該寫while(true) {,因爲你已經檢查了該環路內