-1

I am trying to create a program that quizzes the user on the capitals of the states (I only included 5 states, I will add the rest when it works). I am having trouble with the code to ask the user how many questions they would like to answer. When the program executes, and the user inputs how many questions they would like to answer, it automatically gives the first question and answer before moving on. How do I fix this?

 import java.util.Scanner;

public class lab1 
{
   public static void main(String[] args)
   { 
      //Create scanner object
      Scanner input = new Scanner(System.in);

      //Ask the user how many questions he/she would like to be asked

  System.out.println("How many questions would you like?");
      int numQuest = input.nextInt();

      //Store the 50 states and their capitals in a two-dimensional array
      String[][] states = {{"Alabama", "Montgomery"},
                           {"Alaska", "Juneau"},
                           {"Arizona", "Phoenix"},
                           {"Arkansas", "Little Rock"},
                           {"New York", "Albany"}
                           };

      //Create a for loop that asks a different question every loop
      for(int i = 0; i <= numQuest; i++)
      {
         System.out.print("What is the capital of " + states[i][0] + "? ");//Asks user question
         String answer = input.nextLine();//Allows user to answer

         //If statement determines whether or not answer is correct
         if(answer.equals(states[i][1]))
         {
            System.out.println("Your answer is correct.");
         }else
            System.out.println("The correct answer is " + states[i][1] + ".");

      }

   }

}
jontro
  • 9,267
  • 5
  • 37
  • 65

1 Answers1

0

Just add input.nextLine(); after int numQuest = input.nextInt();

import java.util.Scanner;

public class lab1 
{
   public static void main(String[] args)
   { 
      //Create scanner object
      Scanner input = new Scanner(System.in);

      //Ask the user how many questions he/she would like to be asked

      System.out.println("How many questions would you like?");
      int numQuest = input.nextInt();
      input.nextLine();
      //Store the 50 states and their capitals in a two-dimensional array
      String[][] states = {{"Alabama", "Montgomery"},
                           {"Alaska", "Juneau"},
                           {"Arizona", "Phoenix"},
                           {"Arkansas", "Little Rock"},
                           {"New York", "Albany"}
                           };

      //Create a for loop that asks a different question every loop
      for(int i = 0; i <= numQuest; i++)
      {
         System.out.print("What is the capital of " + states[i][0] + "? ");//Asks user question
         String answer = input.nextLine();//Allows user to answer

         //If statement determines whether or not answer is correct
         if(answer.equals(states[i][1]))
         {
            System.out.println("Your answer is correct.");
         }else
            System.out.println("The correct answer is " + states[i][1] + ".");

      }

   }

}
aswzen
  • 968
  • 14
  • 34