I have been looking online and found a few people who were able to get this sort of code done but nothing provided a solution for the issue i am seeing directly. The task here in this is for the program to ask a user "What is the capital of Arizona". The user needs to enter a capital such as Phoenix and it will award a point and move to the next question all the while tallying the number of correct answered and displaying them at the bottom of the output when done. When running my program it asks the capital of the first state on the list. Once done it will provide a point but then spits out the rest of the states saying that they are correct but does not give the use the chance to enter the next state. Its as if I am missing the part that would move back to the original question of "Enter the capital of the state listed" when the first state has been answered.. Can someone assist? I believe I need a statement like if, else if, and else but cannot see how I would apply it in here or if I should be trying another loop. I couldn't get a do loop to work properly. Any suggestions? My code is below.
import java.util.Scanner;
public class StatesArray {
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) {
int correctStates = 0;
String userInput = "";
int i = 0;
final int NUMBER_OF_US_STATES = 49;
String capitals [][] = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"Georgia", "Atlanta"},
{"Hawaii", "Honolulu"},
{"Idaho", "Boise"},
{"Illinois", "Springfield"},
{"Indiana", "Indianapolis"},
{"Iowa", "Des Moines"},
{"Kansas", "Topeka"},
{"Kentucky", "Frankfort"},
{"Louisiana", "Baton Rouge"},
{"Maine", "Augusta"},
{"Maryland", "Annapolis"},
{"Massachusettes", "Boston"},
{"Michigan", "Lansing"},
{"Minnesota", "Saint Paul"},
{"Mississippi", "Jackson"},
{"Missouri", "Jefferson City"},
{"Montana", "Helena"},
{"Nebraska", "Lincoln"},
{"Nevada", "Carson City"},
{"New Hampshire", "Concord"},
{"New Jersey", "Trenton"},
{"New York", "Albany"},
{"New Mexico", "Santa Fe"},
{"North Carolina", "Raleigh"},
{"North Dakota", "Bismark"},
{"Ohio", "Columbus"},
{"Oklahoma", "Oklahoma City"},
{"Oregon", "Salem"},
{"Pennslyvania", "Harrisburg"},
{"Rhode Island", "Providence"},
{"South Carolina", "Columbia"},
{"South Dakota", "Pierre"},
{"Tennessee", "Nashville"},
{"Texas", "Austin"},
{"Utah", "Salt Lake City"},
{"Vermont", "Montpelier"},
{"Virginia", "Richmond"},
{"Washington", "Olympia"},
{"West Virginia", "Charleston"},
{"Wisconsin", "Madison"},
{"Wyoming", "Cheyenne"}
};
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
i = 0;
for(i = 0; i <= NUMBER_OF_US_STATES; i++){
if(capitals[i][1].equals(userInput)) {
correctStates++;
System.out.println("Yes that is the capital of " + capitals[i][0] + ".");
}
else {
System.out.println("No that is not the capital of " + capitals[i][0] + ".");
i++;
}
}
//counts the number of correct guesses.
System.out.println("Total number of capitals answered correctly: " + correctStates + ".");
i++;
return;
}
}
You have to move the question and input inside the for loop, you are only asking for the input once, and if that is correct then it will print all are correct, or the other way. The two lines below should be inside the for loop.
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
And I think there are 50 states (not 49)? Also if you change to 50, your condition should be < NUMBER_OF_STATES
not <=, otherwise you will have an ArrayIndexOutOfBounds.