Ashley
Ashley

Reputation: 43

Trouble getting my while loop to work

I'm currently working on this assignment for a class and I'm having a hard time getting my while loop to work. Can anyone assist me on figuring out why I can't get the user to enter y or n to either restart the loop or terminate it? Thank you so much!

import java.util.Scanner;
import java.text.NumberFormat;

public class EvenQuizzes {
   public static void main (String[] args) {  

   String another="y";  
   double percent;
   int answers;
   int score = 0;


   Scanner scan = new Scanner(System.in);
   NumberFormat fmt = NumberFormat.getPercentInstance();



   // Asks the user to input the amount of questions on the quiz
   System.out.print("How many questions are on the quiz? ");
   final int QUESTIONS = scan.nextInt();

   System.out.println(); // spacer

   int[] key = new int [QUESTIONS];



   // Asks the user to enter the key
   for (int i=0; i<key.length; i++){
      System.out.print("Enter the correct answer for question "+ (i+1) +": ");
      key[i] = scan.nextInt();
      }

      System.out.println(); // spacer

  while (another.equalsIgnoreCase("y")) {

    // Asks the user to enter their answers 
    for (int i=0; i<key.length; i++) {
      System.out.print("Student's answer for question " + (i+1) + ": " );
      answers = scan.nextInt();

      if (answers == key[i])  
         score++;    
      }


     // Grades the amount of questions right and gives the percentage
      percent = (double)score/QUESTIONS;


      System.out.println();
      System.out.println("Your number of correct answers is: " + score);
      System.out.println("Your quiz percentage: " + fmt.format(percent)); 



      System.out.println();
      System.out.println("Grade another quiz? (y/n)");
      another = scan.nextLine();

      }           
   }
}  

Upvotes: 0

Views: 49

Answers (3)

William Ku
William Ku

Reputation: 798

It has to do with that your scan is reading integers before getting the y/n from the user.

In this transition, the scan is reading a newline character instead of y. As a result, the another is a newline char and hence fails the while-loop condition.

To overcome this, the shortcut method is what @3kings had mentioned. Another method is scan all inputs as string (nextLine) and then for the quiz part, parse for integer. It may seems a lot of work but you get to use parseInt and try/catch exception.

Upvotes: 0

3kings
3kings

Reputation: 838

So instead of this

for (int i=0; i<key.length; i++) {
  System.out.print("Student's answer for question " + (i+1) + ": " );
  answers = scan.nextInt();

  if (answers == key[i])  
     score++;    
  }

you should try this

for (int i=0; i<key.length; i++) {
      System.out.print("Student's answer for question " + (i+1) + ": " );
      answers = scan.nextInt();

      if (answers == key[i])  
         score++;    
      }
    scan.nextLine();

Upvotes: 1

Andy Guibert
Andy Guibert

Reputation: 42926

At the end of your while loop, try adding this print statement:

System.out.println("Next line is >>>" + another + "<<<");

That should make it clear what you are getting from the scan.nextLine() call. It won't fix your problem, but it will make the issue obvious.

Upvotes: 1

Related Questions