2013-03-18 118 views
0

對此處鏈接的前一個問題(https://stackoverflow.com/questions/15487706/try-and-catch-block-not-functioning-in-user-defined-method)的迴應,我對該問題的初步解答已得到解決,但是當它涉及到後來解決的循環時,我遇到了另一個問題通過簡單地使用for循環。但是,我的問題是我不希望用戶在處理異常之後不得不重新啓動程序,而是希望它將相同的開始問題循環到用戶。我試着在返回語句之後放置打印語句,並嘗試在try catch之後完全複製邏輯代碼,但是,意識到這不會導致用戶爲異常循環無限次。另外,在附註中,是的,我之前的問題有很好的答案,然而,沒有人設法回答我更多反覆出現的問題,這就是爲什麼沒有人得到答覆的答案。如何使異常處理循環(無限次數)

import java.io.*; 
import java.text.DecimalFormat; 

public class Test 
{ 

    public static void main(String[] args) throws IOException 
    { 

     double x; 
     x = circlemethods(0.0, 0.0, 0.0, 1.0); 

    } 

    public static double circlemethods(double volume, double surfacearea, 
      double area, double radius) throws IOException 
    { 

     BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in)); 

     String numInput; 
     String reqInput; 
     String amountStr; 
     double numInt = 0; 
     double num = 0; 
     double answer = 0; 
     double amount = 0; 
     double answer2 = 0; 
     double answer3 = 0; 
     double answer4 = 0; 

     for (double i = 0; i < 999; i++) 
      ; 
     try 
     { 

      // for (double i = 0; i < 999; i++); 

      // while (numInt != 999) { 

      System.out.println("This program will ask for a given user radius, then proceed to calculate the user input"); 
      System.out.println("The program will use four methods to achieve this, all calling back to the main method"); 
      System.out.println("Press any key to continue"); 
      numInput = myInput.readLine(); 

      System.out.println("First, what would you like to calculate?"); 
      System.out.println("Enter '1' for Circumference, '2' for area, '3' for volume, or '4' for surface area"); 
      reqInput = myInput.readLine(); 
      numInt = Double.parseDouble(reqInput); 

      System.out.println("Now enter the radius of the required shape(Half of diameter)"); 
      numInput = myInput.readLine(); 
      num = Double.parseDouble(numInput); 

      DecimalFormat nextAmount = new DecimalFormat("0.00"); 
      amountStr = nextAmount.format(amount); 

      if (numInt == 1) 
      { 
       System.out.println("You chose to calculate circumference, given the radius :" + num); 
       answer = (3.14) * (2) * (num); 
       System.out.print("The circumference of that sphere is :"); 
       System.out.println(answer + "cm³"); 
       return answer; 
      } 
      else if (numInt == 2) 
      { 
       System.out.println("You chose to calculate area, given the radius :" + num); 
       answer2 = (3.14) * 2; 
       System.out.print("The area of the circle is :"); 
       System.out.println(answer2 + "cm²"); 
       return answer2; 
      } 
      else if (numInt == 3) 
      { 
       System.out.println("You chose to calculate volume, given the radius :" + num); 
       answer3 = 4/3 * (3.14) * (num) * (3) * (3) * (3); 
       System.out.print("The volume of that sphere is : cm³"); 
       System.out.println(answer3 + "cm³"); 
       return answer3; 
      } 
      else 
      // if (numInt == 4) 
      { 
       System.out.println("You chose to calculate surface area, given the radius :" + num); 
       answer4 = 4 * (3.14) * (num) * (2) * (2); 
       System.out.print("The Surface area of that sphere is :"); 
       System.out.println(answer4 + "cm²"); 
       return answer4; 

      } 

     } catch (Exception e) 
     { 
      System.out.println("Please do not enter any string values, next time input enter a number "); 
      return 0; 
      // how to loop this untill the user inputs a number???? 

     } 

    } 

} 
+2

您需要改進您的代碼格式,因爲在編寫代碼時很難閱讀和理解您的代碼。當要求他人幫助您使用此代碼時,這一點尤其重要。考慮創建一個獲取輸入的方法,在方法中有一個while循環,除非輸入有效,否則不會從方法返回。 – 2013-03-18 23:49:17

回答

0

首先,您使用while(true)循環,而不是循環999次。 (考慮到它後面有分號,該循環實際上沒有做任何事情。)

然後,您從catch塊中刪除return 0;塊。

因此,這將是

while(true) { 
    try { 
     .... //your code 
    } 
catch {...} 
} //while loop end bracket 

這樣,如果它達到你return語句的一個循環纔會結束。

+0

這兩個答案都是正確的,但我確實尊重您的努力和Memento的努力,但是,您的工作最快,非常感謝! – user2184171 2013-03-19 00:15:47

0

您需要循環,直到獲得有效值。一種方法是循環直到布爾值被設置爲true。嘗試與此類似:

boolean inputIsValid = false; 
while (!inputIsValid) { ... 

然後當你已經確定你有一個有效的輸入,添加一行:

inputIsValid = true; 

你的循環將繼續下去,直到inputIsValid爲真。

你也可以創建一個無盡的while循環。然後,當你收到有效輸入,break圈外:

while(true) { 
    //when valid input is received 
    break; 
}