2016-02-19 45 views
0

好的,謝謝,我已經計算出了退出循環。程序結束時出現循環問題,無法退出

現在我的問題是,我試圖測試int newScore,以確保其值在1和100之間。該循環的一半工作。如果我在參數外輸入一個值,它會循環並再次請求它。然而,當我在參數中輸入一個值時,它不會打破循環並繼續到程序的下一部分(要麼提出下一個測試分數,要麼計算平均值,如果沒有更多分數)這是我更新的代碼試試這個循環:

 do { 
 
      while (scoreValid) { 
 
      while (tv) { 
 
       try { 
 
       System.out.println("Please enter test score # " + counter); 
 
       newScore = Integer.parseInt(scan.nextLine()); 
 
       if (newScore >= 1 && newScore <= 100) { 
 
        scoreValid = true; 
 
        tv = false; 
 
        counter++; 
 
        totalScore = totalScore + newScore; 
 
        break; 
 
       } else { 
 

 
        System.out.println("Sorry, you must enter a number between 1 and 100."); 
 
        tv = true; 
 
        continue; 
 
       } 
 

 
       } catch (Exception e) { 
 
       System.out.println("Sorry, I didn't catch that"); 
 
       continue; 
 
       } 
 
      } 
 
      } 
 
     } while (counter <= numberTests);

這裏是原代碼....

/** 
 
* This app will average test scores and tell you what letter grade you received. 
 
* 
 
* @jordan.hawkes 
 
* @1.0 
 
*/ 
 

 
import java.util.Scanner; 
 
public class TestScoreCalculator { 
 
    public static void main(String[] args) { 
 

 
     String first; // First Name entered by user 
 
     String last; // Last Name entered by user 
 
     String newTestAnswer; // answer for entering another test score 
 
     String redoAnswer; // answer to rerun program 
 
     int avg; // Average test score 
 
     int totalScore = 0; // Total Running Test Score 
 
     int newScore; // Test Score Entered by User 
 
     int counter = 1; // Number of Scores Entered 
 
     int testNumber; // Test # counter 
 
     int numberTests = 0; // Number of Tests entered by user 
 
     boolean scoreValid = false; // boolean to validate score  
 
     boolean newTest = false; // boolean to validate entering another test 
 
     boolean redo = false; // boolean to rerun program 
 
     boolean numberTestsValid = false; // boolean to validate number of tests 
 
     boolean test = false; // test boolean to validate exit/rerun 
 

 
     Scanner scan = new Scanner(System.in); 
 

 
     System.out.print("Please enter your first name: "); 
 
     first = scan.nextLine(); 
 

 
     System.out.print("Hello, " + first + ", please enter your last name: "); 
 
     last = scan.nextLine(); 
 

 
     while (redo = true) { 
 
     while (numberTestsValid = true) { 
 
      try { 
 
      System.out.println("Thanks, " + first + ", how many test scores would you like to enter? "); 
 
      numberTests = Integer.parseInt(scan.nextLine()); 
 
      numberTestsValid = true; 
 
      break; 
 
      } catch (Exception e) { 
 
      System.out.println("Sorry, I didn't catch that"); 
 
      continue; 
 
      } 
 
     } 
 

 
     do { 
 
      while (scoreValid = true) { 
 
      try { 
 
       System.out.println("Great, please enter test score # " + counter); 
 
       newScore = Integer.parseInt(scan.nextLine()); 
 
       scoreValid = true; 
 
       counter++; 
 
       totalScore = totalScore + newScore; 
 
       break; 
 
      } catch (Exception e) { 
 
       System.out.println("Sorry, I didn't catch that"); 
 
       continue; 
 
      } 
 
      } 
 
     } while (counter <= numberTests); 
 

 
     testNumber = counter - 1; 
 
     avg = (totalScore/testNumber); 
 

 
     switch (avg/10) { 
 
      case 10: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an A+!"); 
 
      break; 
 
      case 9: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an A!"); 
 
      break; 
 
      case 8: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a B!"); 
 
      break; 
 
      case 7: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a C!"); 
 
      break; 
 
      case 6: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got a D!"); 
 
      break; 
 
      default: 
 
      System.out.println("Congratulations " + first.charAt(0) + ". " + last + ", you got an F!"); 
 

 
     } 
 

 
     while (test = true) { 
 
      System.out.println("type r to restart or q to quit"); 
 
      redoAnswer = scan.nextLine().trim().toLowerCase(); 
 
      if (redoAnswer.equals("r")) { 
 
      test = false; 
 
      totalScore = 0; 
 
      counter = 1; 
 
      redo = true; 
 
      break; 
 
      } else if (redoAnswer.equals("q")) { 
 
      test = false; 
 
      redo = false; 
 
      System.out.println("Goodbye!"); 
 
      continue; 
 
      } else { 
 
      System.out.println("Sorry, I didn't catch that. Please type r to restart or q to quit"); 
 
      continue; 
 
      } 
 
     } 
 
     } 
 
    } //end main 
 
} //end class

回答

0

經典的錯誤:

while (redo = true) 

分配trueredo並且總是true,它並沒有測試它。

它更改爲

while (redo == true) 

或好得多:

while (redo) 

同去的其他環條件下。

+0

哦,非常感謝你,哇,不知道我是如何錯過了,但我很感激它,並改變了所有的循環條件!感謝您的解釋,所以我不知道未來會這樣做。 我有另一個問題,我想知道... 所以當我要求用戶輸入測試分數,然後我解析答案,以確保它是一個int,有沒有辦法設置他們可以輸入的值的邊界?例如,在測試分數中,我只想要1到100之間的答案。 –

+0

解析它,然後測試它,也許'scoreValid = newScore> 0 && newScore <101'或其他。 – Bohemian