Write a program that repeatedly prompts the user to enter a capital for a state.
ID: 3778838 • Letter: W
Question
Write a program that repeatedly prompts the user to enter a capital for a state. Upon receiving the user input, the program reports whether the answer is correct. Assume that 50 states and their capitals are stored in a two-dimensional array, as shown in Figure 8.10. The program prompts the user to answer all states capitals and displays the total correct count. The user's answer is not case-sensitive. A two-dimensional array stores states and their capitals. Here is a sample run: What is teh capital of Alabama? Montogomery What is the capital of Alaska? JuneauExplanation / Answer
#JAVA
import java.util.Scanner;
public class StatesAndItsCapitals {
String[][] statesCapitals={{"Tamil Nadu","Chennai"},{"Andhra Pradesh","Amaravathi"},{"Telangana","Hyderabad"},{"GOA","PANAJI"}}; //List of States and its capitals
Scanner sc=null;
public static void main(String[] args) {
StatesAndItsCapitals stsAndCaps=new StatesAndItsCapitals();
stsAndCaps.promptUser();
}
void promptUser()
{
sc=new Scanner(System.in);
String readCapital="";
for(int i=0;i<statesCapitals.length;i++){
for(int j=0;j<statesCapitals.length;j++){
System.out.println("What is the Capital of "+statesCapitals[i][j]+" ?");
readCapital=sc.nextLine();
if(readCapital.equalsIgnoreCase(statesCapitals[i][j+1])){
System.out.println("Your answer is correct");
break;
}
else{
System.out.println("Your answer should be "+statesCapitals[i][j+1]);
break;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.