Home > Back-end >  How do I fix this Java Loop Scanner
How do I fix this Java Loop Scanner

Time:10-05

This is part of a code that I am running and when the code runs it lets me enter a number but will not let me move on, it will just continue to print "Enter a student's total quiz score : " even though i have entered a value less than the perfect quiz score help!

  while (perfectQuizScore > quizScore ) {
        System.out.print("Enter a student's total quiz score : ");
        quizScore = Integer.parseInt(keyboard.nextLine()); // 6
        if (quizScore > perfectQuizScore) {
            System.out.println("Invalid value! Score must not be greater than perfect score.");
        }
        else if (quizScore < 0) {
            System.out.println("Invalid value! Value must be greater than 0.");
        }

    }

OUTPUT: enter image description here

REVERSING THE SIGNS enter image description here

CodePudding user response:

If I'm understanding your question correctly... you want to exit the loop when the supplied quiz score is less than (or equal to) the perfect score? If so, you just need to change the sign in your while loop conditional (it is currently checking greater than).

Before: while (perfectQuizScore > quizScore ) {

After: while (perfectQuizScore < quizScore ) {

Now this is assuming that you initially set quizScore to a huge value (if you initialize it to 0 then the conditional will evaluate to false before entering the loop). A better solution is probably to use a do-while loop.

int quizScore = 0;
do {
   System.out.print("Enter a student's total quiz score : ");
   quizScore = Integer.parseInt(keyboard.nextLine()); // 6
   if (quizScore > perfectQuizScore) {
      System.out.println("Invalid value! Score must not be greater than perfect score.");
   }
   else if (quizScore < 0) {
      System.out.println("Invalid value! Value must be greater than 0.");
   }
} while (perfectQuizScore < quizScore or quizScore < 0);

Although I would personally probably structure this differently, to avoid writing your "bad" conditions down twice:

int quizScore = 0;
while (True) {
   System.out.print("Enter a student's total quiz score : ");
   quizScore = Integer.parseInt(keyboard.nextLine()); // 6
   if (quizScore > perfectQuizScore) {
      System.out.println("Invalid value! Score must not be greater than perfect score.");
   }
   else if (quizScore < 0) {
      System.out.println("Invalid value! Value must be greater than 0.");
   }
   else {
      // good input, exit the loop with a break statement
      break;
   }
}

CodePudding user response:

Maybe the following is what you searching:

    do {
      System.out.print("Enter a student's total quiz score : ");
      quizScore = scnr.nextInt();
      if (quizScore > perfectQuizScore) {
          System.out.println("Invalid value! Score must not be greater than perfect score.");
      }
      else if (quizScore <= 0) {
          System.out.println("Invalid value! Value must be greater than 0.");
      } else {
          break; // get out of the loop
      }
    } while ( quizScore <= 0 || quizScore > perfectQuizScore );

Do the check (while) after getting the first time, also change the checks to stay on the loop until the user write a valid value.

The do {...} will let the code inside run one time before the while checks.

  •  Tags:  
  • java
  • Related