This question has been posted before, but NOT with a properly corrected answer t
ID: 3854800 • Letter: T
Question
This question has been posted before, but NOT with a properly corrected answer that gives expected output. Can anyone answer this and provide a correct java answer?
Modify the program below to prompt the user enter the capital for a state. Upon receiving the user input, the program reports whether the answer is correct. Create and complete a two-dimensional array to store the 50 states and their capitals. Display all 50 states and ask the user to enter its capital, one at a time. Compare the user input with the array. Count the correct answers. Display the total correct count. Example: What is the capital for Alabama? What is the capital for Alaska? ... ... ... What is the capital for Delaware? ... ... ... Total correct count: 35 If the user entered a correct answer, display the next question; If the user entered a wrong answer for Delaware, display a message... Sorry, the capital for Delaware is Dover. Note: 1.The user's answer is not case-sensitive (equalsIgnoreCase) Use println
import java.util.Scanner; public class StateCapital { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); /* FIXME: Delcare... 1. A string variable to store the user input 2. An integer variable to count the total correct answers */ // FIXME: Initialize a multi-dimensional string array // FIXME: Complete the array to store the state and capital names stateCapital[0][0] = "Alabama"; stateCapital[0][1] = "Montgomery"; stateCapital[1][0] = "Alaska"; stateCapital[1][1] = "Juneau"; stateCapital[2][0] = "Arizona"; stateCapital[2][1] = "Phoenix"; stateCapital[3][0] = "Arkansas"; stateCapital[3][1] = "Little Rock"; stateCapital[4][0] = "California"; stateCapital[4][1] = "Sacramento"; stateCapital[5][0] = "Colorado"; stateCapital[5][1] = "Denver"; stateCapital[6][0] = "Connecticut"; stateCapital[6][1] = "Hartford"; stateCapital[7][0] = "Delaware"; stateCapital[7][1] = "Dover"; stateCapital[8][0] = "Florida"; stateCapital[8][1] = "Tallahassee"; stateCapital[9][0] = "Georgia"; stateCapital[9][1] = "Atlanta"; stateCapital[20][0] = "Massachusetts"; stateCapital[20][1] = "Boston"; stateCapital[21][0] = "Michigan"; stateCapital[21][1] = "Lansing"; stateCapital[22][0] = "Minnesota"; stateCapital[22][1] = "St. Paul"; stateCapital[23][0] = "Mississippi"; stateCapital[23][1] = "Jackson"; stateCapital[24][0] = "Missouri"; stateCapital[24][1] = "Jefferson City"; stateCapital[25][0] = "Montana"; stateCapital[25][1] = "Helena"; stateCapital[26][0] = "Nebraska"; stateCapital[26][1] = "Lincoln"; stateCapital[27][0] = "Nevada"; stateCapital[27][1] = "Carson City"; stateCapital[28][0] = "New Hampshire"; stateCapital[28][1] = "Concord"; stateCapital[29][0] = "New Jersey"; stateCapital[29][1] = "Trenton"; stateCapital[40][0] = "South Dakota"; stateCapital[40][1] = "Pierre"; stateCapital[41][0] = "Tennessee"; stateCapital[41][1] = "Nashville"; stateCapital[42][0] = "Texas"; stateCapital[42][1] = "Austin"; stateCapital[43][0] = "Utah"; stateCapital[43][1] = "Salt Lake City"; stateCapital[44][0] = "Vermont"; stateCapital[44][1] = "Montpelier"; stateCapital[45][0] = "Virginia"; stateCapital[45][1] = "Richmond"; stateCapital[46][0] = "Washington"; stateCapital[46][1] = "Olympia"; stateCapital[47][0] = "West Virginia"; stateCapital[47][1] = "Charleston"; stateCapital[48][0] = "Wisconsin"; stateCapital[48][1] = "Madison"; stateCapital[49][0] = "Wyoming"; stateCapital[49][1] = "Cheyenne"; // FIXME: Ask the user to guess the capitals for all 50 states, display one state at a time; Use for loop System.out.println("What is the capital for Delaware?"); // FIXME: Modify the statement below to print total correct count System.out.println("Total correct count: 35"); } }
Explanation / Answer
Here is the code for the question. In case of any issues, post a comment, I will respond. If happy with the answer, please rate it. Thank you.
import java.util.Scanner;
public class StateCapital {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// Delcare... 1. A string variable to store the user input
String answer;
//An integer variable to count the total correct answers
int correct = 0;
// Initialize a multi-dimensional string array
String stateCapital[][] = new String[50][2];
// the array to store the state and capital names
stateCapital[0][0] = "Alabama"; stateCapital[0][1] = "Montgomery";
stateCapital[1][0] = "Alaska"; stateCapital[1][1] = "Juneau";
stateCapital[2][0] = "Arizona"; stateCapital[2][1] = "Phoenix";
stateCapital[3][0] = "Arkansas"; stateCapital[3][1] = "Little Rock";
stateCapital[4][0] = "California"; stateCapital[4][1] = "Sacramento";
stateCapital[5][0] = "Colorado"; stateCapital[5][1] = "Denver";
stateCapital[6][0] = "Connecticut"; stateCapital[6][1] = "Hartford";
stateCapital[7][0] = "Delaware"; stateCapital[7][1] = "Dover";
stateCapital[8][0] = "Florida"; stateCapital[8][1] = "Tallahassee";
stateCapital[9][0] = "Georgia"; stateCapital[9][1] = "Atlanta";
stateCapital[10][0] = "Hawaii"; stateCapital[10][1] = "Honolulu";
stateCapital[11][0] = "Idaho"; stateCapital[11][1] = "Boise";
stateCapital[12][0] = "Illinois"; stateCapital[12][1] = "Springfield";
stateCapital[13][0] = "Indiana"; stateCapital[13][1] = "Indianapolis";
stateCapital[14][0] = "Iowa"; stateCapital[14][1] = "Des Moines";
stateCapital[15][0] = "Kansas"; stateCapital[15][1] = "Topeka";
stateCapital[16][0] = "Kentucky"; stateCapital[16][1] = "Frankfort";
stateCapital[17][0] = "Louisiana"; stateCapital[17][1] = "Baton Rouge";
stateCapital[18][0] = "Maine"; stateCapital[18][1] = "Augusta";
stateCapital[19][0] = "Maryland"; stateCapital[19][1] = "Annapolis";
stateCapital[20][0] = "Massachusetts"; stateCapital[20][1] = "Boston";
stateCapital[21][0] = "Michigan"; stateCapital[21][1] = "Lansing";
stateCapital[22][0] = "Minnesota"; stateCapital[22][1] = "St. Paul";
stateCapital[23][0] = "Mississippi"; stateCapital[23][1] = "Jackson";
stateCapital[24][0] = "Missouri"; stateCapital[24][1] = "Jefferson City";
stateCapital[25][0] = "Montana"; stateCapital[25][1] = "Helena";
stateCapital[26][0] = "Nebraska"; stateCapital[26][1] = "Lincoln";
stateCapital[27][0] = "Nevada"; stateCapital[27][1] = "Carson City";
stateCapital[28][0] = "New Hampshire"; stateCapital[28][1] = "Concord";
stateCapital[29][0] = "New Jersey"; stateCapital[29][1] = "Trenton";
stateCapital[30][0] = "New Mexico"; stateCapital[30][1] = "Santa Fe";
stateCapital[31][0] = "New York"; stateCapital[31][1] = "Albany";
stateCapital[32][0] = "North Carolina"; stateCapital[32][1] = "Raleigh";
stateCapital[33][0] = "North Dakota"; stateCapital[33][1] = "Bismarck";
stateCapital[34][0] = "Ohio"; stateCapital[34][1] = "Columbus";
stateCapital[35][0] = "Oklahoma"; stateCapital[35][1] = "Oklahoma City";
stateCapital[36][0] = "Oregon"; stateCapital[36][1] = "Salem";
stateCapital[37][0] = "Pennsylvania"; stateCapital[37][1] = "Harrisburg";
stateCapital[38][0] = "Rhode Island"; stateCapital[38][1] = "Providence";
stateCapital[39][0] = "South Carolina"; stateCapital[39][1] = "Columbia";
stateCapital[40][0] = "South Dakota"; stateCapital[40][1] = "Pierre";
stateCapital[41][0] = "Tennessee"; stateCapital[41][1] = "Nashville";
stateCapital[42][0] = "Texas"; stateCapital[42][1] = "Austin";
stateCapital[43][0] = "Utah"; stateCapital[43][1] = "Salt Lake City";
stateCapital[44][0] = "Vermont"; stateCapital[44][1] = "Montpelier";
stateCapital[45][0] = "Virginia"; stateCapital[45][1] = "Richmond";
stateCapital[46][0] = "Washington"; stateCapital[46][1] = "Olympia";
stateCapital[47][0] = "West Virginia"; stateCapital[47][1] = "Charleston";
stateCapital[48][0] = "Wisconsin"; stateCapital[48][1] = "Madison";
stateCapital[49][0] = "Wyoming"; stateCapital[49][1] = "Cheyenne";
// Ask the user to guess the capitals for all 50 states, display one state at a time; Use for loop
for(int i = 0; i < 50; i++)
{
System.out.print("What is the capital for " + stateCapital[i][0] + "? ");
answer = scnr.nextLine().trim();
if(!answer.equalsIgnoreCase(stateCapital[i][1]))
{
System.out.println("Sorry, the capital of " + stateCapital[i][0] + " is " +stateCapital[i][1] + ".");
}
else
{
correct++;
}
}
// print total correct count
System.out.println("Total correct count: " + correct);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.