Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Netbeans Write a program that repeatedly prompts the user in enter a

ID: 3778942 • Letter: P

Question


Please use Netbeans

Write a program that repeatedly prompts the user in enter a capital for a sate. Upon receiving the user input, the program repeats whether the answer is correct. Assume that %0 state and their capitals are stored in a two decimal array, as shown in Figure 8.10. The program prompt the user to answer all states' capitals and displays the total correct count. The user's answer is not case-sensitive. Alabama Montgonery Alaska Juneau Arizona Phoenix A two-decimal array stores agate and their capitals, Here is a sample run: What is the capital of Albama? Montogonery The correct answer should be Montgonery What is the capital of Alaska? Juneau Ypur answer is correct What is the capital of Arizona?. .. The correct count is 35

Explanation / Answer

import java.util.Scanner;

public class StateCapitalDemo {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       // 2d array of 50 states and capitals
       String[][] statesAndCapitols = {
               { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware",
                       "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
                       "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",
                       "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",
                       "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
                       "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
                       "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" },
               { "Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover",
                       "Tallahassee", "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines",
                       "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis", "Boston", "Lansing", "St. Paul",
                       "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton",
                       "Santa Fe", "Albany", "Raleigh", "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg",
                       "Providence", "Columbia", "Pierre", "Nashville", "Austin", "Salt Lake City", "Montpelier",
                       "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne" } };
      
      

   int correctCount=0;
   //loop through array
   for (int i = 0; i < statesAndCapitols[0].length; i++) {
           System.out.println("What is the capital of "+statesAndCapitols[0][i]);
           String answer=sc.nextLine();
           if(answer.equalsIgnoreCase(statesAndCapitols[1][i])){
               correctCount++; // incrementing the correct count by 1
               System.out.println("Your answer is correct");
           }else{
               System.out.println("The correct answer should be "+statesAndCapitols[1][i]);
           }
       }
  
   System.out.println("The correct count is "+correctCount); // printing the total correct count
}

  
}