Question
Hi, I'm having a bit of trouble with Java, getting the correct output for a program in which I'm meant to modify the code to
Hi, I'm having a bit of trouble with Java, getting the correct output for a program in which I'm meant to modify the code to prompt the user enter the capital for a state. Upon receiving the user input, the program reports whether the answer is correct. The instructions are "1.Create and complete a two-dimensional array to store the 50 states and their capitals, 2. Display all 50 states and ask the user to enter its capital, one at a time, 3. Compare the user input with the array. Count the correct answers, 4. Display the total correct count.
This question has been asked here before, but the other examples of code did not work for my inter-active online book.
I think my code is almost right, here it is:
import java.util.Scanner; public class StateCapital { public static void main(String[] args) { Scanner input = new Scanner(System.in); int correctCount = 0; for (int i = 0; i < stateCapitals.length; i++) { System.out.println("What is the capital for " + stateCapitals[i][0]+"?"); String s = input.nextLine(); if (s.equalsIgnoreCase(stateCapitals[i][1])) { correctCount++; } else { System.out.println("Sorry, the capital for " + stateCapitals[i][0] + " is " + stateCapitals[i][1] + "."); } } System.out.println("Total correct count: " + correctCount); }
private static String[][] stateCapitals = { {"Alabama", "Montgomery"}, {"Alaska", "Juneau"}, {"Arizona", "Phoenix"}, {"Arkansas", "Little Rock"}, {"California", "Sacramento"}, {"Colorado", "Denver"}, {"Connecticut", "Hartford"}, {"Delaware", "Dover"}, {"Florida", "Tallahassee"}, {"Georgia", "Atlanta"}, {"Hawaii", "Honolulu"}, {"Idaho", "Boise"}, {"Illinois", "Springfield"}, {"Maryland", "Annapolis"}, {"Minnesota", "Saint Paul"}, {"Iowa", "Des Moines"}, {"Maine", "Augusta"}, {"Kentucky", "Frankfort"}, {"Indiana", "Indianapolis"}, {"Kansas", "Topeka"}, {"Louisiana", "Baton Rouge"}, {"Oregon", "Salem"}, {"Oklahoma", "Oklahoma City"}, {"Ohio", "Columbus"}, {"North Dakota", "Bismark"}, {"New York", "Albany"}, {"New Mexico", "Santa Fe"}, {"New Jersey", "Trenton"}, {"New Hampshire", "Concord"}, {"Nevada", "Carson City"}, {"Nebraska", "Lincoln"}, {"Montana", "Helena"}, {"North Carolina", "Raleigh"}, {"Missouri", "Jefferson City"}, {"Mississippi", "Jackson"}, {"Massachusetts", "Boston"}, {"Michigan", "Lansing"}, {"Pennsylvania", "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"} }; }
Now in this imgur album is the expected output VS. my output. https://imgur.com/a/YvVMzGs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started