Search code examples
loopswhile-loopbooleaninfinite

loop issue at end of program, can't exit


Ok, thanks, I have figured out the exit loop.

Now my problem is that I'm trying to test the int newScore to ensure its value is between 1 and 100. The loop halfway works. If I enter a value outside the parameters it loops and asks for it again. However, when I enter a value within the parameters it's not breaking the loop and continuing to the next part of the program (either asking for the next test score, or calculating the average if there are no more scores) This is the code I updated to try this loop:

        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);

Here is the original code....

/**
 * 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


Solution

  • Classic bug:

    while (redo = true)
    

    assigns true to redo and is always true, it doesn't test it.

    change it to either

    while (redo == true)
    

    or far better:

    while (redo)
    

    Same goes for other loop conditions.