Question

In: Computer Science

I am wanting to know how to write guessing game program in java that prompts the...

I am wanting to know how to write guessing game program in java that prompts the use to enter a capital for a each of 50 states, one state at a time. Upon receiving the user input, the program reports whether the answer is correct or incorrect. Assume 50 states and their capitals are stored in a two-dimensional array. Attached is a copy of the two-dimensional array that can be copied into your program. The user's answer is not case sensitive.

Sample run:

What is the capital of Alabama? Phoenix

Your answer is incorrect.

What is the capital of Alaska? Juneau

Your answer is correct.

What is the capital of Arizona? phoenix

Your answer is correct.

statecapitals.txt

String[][] stateCapital = {
      {"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"},
      {"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", "Bismarck"},
      {"Ohio", "Columbus"},
      {"Oklahoma", "Oklahoma City"},
      {"Oregon", "Salem"},
      {"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"}
    };

Solutions

Expert Solution

Answer :

import java.util.*;

class Main {

public static void main(String[] args) {

//states and capitals are declared

String[][] stateCapital = {

{"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"},

{"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", "Bismarck"},

{"Ohio", "Columbus"},

{"Oklahoma", "Oklahoma City"},

{"Oregon", "Salem"},

{"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"}

};

//scanner class to read the inputs from user

Scanner scan = new Scanner(System.in);

//loop iterate through each row of state and capital, 50 times

for (int x = 0; x < stateCapital.length; x ++) {

//make one subarray to iterate through state and capitals of particular row

String subStateCapital[] = stateCapital[x];

for (int y = 0; y < subStateCapital.length-1; y ++) {

String stCp = subStateCapital[y];

System.out.print(String.format("What is the capital of %s?",stCp));

//if capital entered is correct then save as true, it will not case sensitive

boolean bool =scan.next().equalsIgnoreCase(subStateCapital[1]);

//print the result according to value of bool

if(bool){

System.out.println("Your answer is correct");

}

else {

System.out.println("Your answer is incorrect");

}

}

}

}

}

Output :


Related Solutions

THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...
THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct. The program should randomly select 10 out of the 50 states. Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays: The length of the string is: xxxxx The strings first character is: x    (where xxxxx is the length of the string and x is the first character.)
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
Java guessing game: For this program you will use Linear Search. - Ask the user to...
Java guessing game: For this program you will use Linear Search. - Ask the user to choose between a number from 0 to 2000. - The user will guess how long the linear search will take in nanoseconds, - After the user puts in their answer, show the user the difference between the actual answer and their answer. (Example: The user puts in 5 nanoseconds. The actual time is 11 nanoseconds. The program will show 6 nanoseconds.) There should be...
I am trying to write a simple game in Java named GuessNumber.java to run on the...
I am trying to write a simple game in Java named GuessNumber.java to run on the command line. This program asks user to pick a number and the computer guesses the number repetitively until the guess is correct and will allow the user to set the lower and upper bound as command line parameters from where the picks the number. If no command line parameters are specified then the program uses a default of 1 to 99.
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT