Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework help for Java: I'm having a hard time understanding the whole block of code. Modify the program below to prompt the user enter the

Homework help for Java: I'm having a hard time understanding the whole block of code.

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) 2. Use println

import java.util.Scanner;

public class StateCapital {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Strubg[][]stateCapital = new String[60][60]; int correctcnt = 0; int i = 0; /* 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 for(int i = 0; i < stateCapital.length; i++) { System.out.println("What is the capital for Alabama?"); System.out.println("What is the capital for Alaska?"); System.out.println("What is the capital for Arizona?"); System.out.println("What is the capital for Arkansas?"); System.out.println("What is the capital for California?"); System.out.println("What is the capital for Colorado?"); System.out.println("What is the capital for Connecticut?"); System.out.println("What is the capital for Delaware?"); System.out.println("What is the capital for Florida?"); System.out.println("What is the capital for Georgia?"); System.out.println("What is the capital for Hawaii?"); System.out.println("What is the capital for Idaho?"); System.out.println("What is the capital for Illinois?"); System.out.println("What is the capital for Indiana?"); System.out.println("What is the capital for Iowa?"); System.out.println("What is the capital for Kansas?"); System.out.println("What is the capital for Kentucky?"); System.out.println("What is the capital for Louisiana?"); System.out.println("What is the capital for Maine?"); System.out.println("What is the capital for Maryland?"); System.out.println("What is the capital for Massachusetts?"); System.out.println("What is the capital for Michigan?"); System.out.println("What is the capital for Minnesota?"); System.out.println("What is the capital for Mississippi?"); System.out.println("What is the capital for Missouri?"); System.out.println("What is the capital for Montana?"); System.out.println("What is the capital for Nebraska?"); System.out.println("What is the capital for Nevada?"); System.out.println("What is the capital for New Hampshire?"); System.out.println("What is the capital for New Jersey?"); System.out.println("What is the capital for New Mexico?"); System.out.println("What is the capital for New York?"); System.out.println("What is the capital for North Carolina?"); System.out.println("What is the capital for North Dakota?"); System.out.println("What is the capital for Ohio?"); System.out.println("What is the capital for Oklahoma?"); System.out.println("What is the capital for Oregon?"); System.out.println("What is the capital for Pennsylvania?"); System.out.println("What is the capital for Rhode Island?"); System.out.println("What is the capital for South Carolina?"); System.out.println("What is the capital for South Dakota?"); System.out.println("What is the capital for Tennessee?"); System.out.println("What is the capital for Texas?"); System.out.println("What is the capital for Utah?"); System.out.println("What is the capital for Vermont?"); System.out.println("What is the capital for Virginia?"); System.out.println("What is the capital for Washington?"); System.out.println("What is the capital for West Virginia?"); System.out.println("What is the capital for Wisconsin?"); System.out.println("What is the capital for Wyoming?"); // FIXME: Modify the statement below to print total correct count } if(stateCapitals.equalsIgnoreCase(stateCapital[i][1])) { System.out.println(stateCapital); } else { System.out.println("Sorry, the capital for System.out.println("Total correct count: 35"); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

What is a firm-level strategy?

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago