第八章第三十七题(猜测首府)(Guess the capital)
-
**8.37(猜测首府)编写一个程序,重复提示用户一个州的首府。当接收到用户输入后,程序报告答案是否正确。假设50个州以及他们的首府保存在一个二维数组中,。程序提示用户回答所有州的首府,并且显示所有正确答案的数目(忽略英文字母的大小写)。下面是一个运行示例。
What is the capital of Alabama?Montgomey
Your answer is correct
What is the capital of Alaska?Juneau
Your answer is correct
What is the capital of Arizona?Phoenix
Your answer is correct
What is the capital of Arkansas?q
The correct answer should be Little Rock
What is the capital of California?s
The correct answer should be Sacramento
What is the capital of Colorado?s
The correct answer should be Denver
What is the capital of Connecticut?s
The correct answer should be Hartford
What is the capital of Delaware?s
The correct answer should be Dover
What is the capital of Florida?s
The correct answer should be Tallahassee
What is the capital of Georgia?s
The correct answer should be Atlanta
What is the capital of Hawaii?s
The correct answer should be Honolulu
What is the capital of Idaho?s
The correct answer should be Boise
What is the capital of Illinois?s
The correct answer should be Springfield
What is the capital of Indiana?s
The correct answer should be Indianapolis
What is the capital of Iowa?s
The correct answer should be Des Moines
What is the capital of Kansas?s
The correct answer should be Topeka
What is the capital of Kentucky?s
The correct answer should be Frankfort
What is the capital of Louisiana?s
The correct answer should be Baton Rouge
What is the capital of Maine?s
The correct answer should be Augusta
What is the capital of Maryland?s
The correct answer should be Annapolis
What is the capital of Massachusettes?s
The correct answer should be Boston
What is the capital of Michigan?s
The correct answer should be Lansing
What is the capital of Minnesota?s
The correct answer should be Saint Paul
What is the capital of Mississippi?s
The correct answer should be Jackson
What is the capital of Missouri?s
The correct answer should be Jefferson City
What is the capital of Montana?s
The correct answer should be Helena
What is the capital of Nebraska?s
The correct answer should be Lincoln
What is the capital of Nevada?s
The correct answer should be Carson City
What is the capital of New Hampshire?s
The correct answer should be Concord
What is the capital of New Jersey?s
The correct answer should be Trenton
What is the capital of New York?s
The correct answer should be Albany
What is the capital of New Mexico?s
The correct answer should be Santa Fe
What is the capital of North Carolina?s
The correct answer should be Raleigh
What is the capital of North Dakota?s
The correct answer should be Bismark
What is the capital of Ohio?s
The correct answer should be Columbus
What is the capital of Oklahoma?s
The correct answer should be Oklahoma City
What is the capital of Oregon?s
The correct answer should be Salem
What is the capital of Pennslyvania?s
The correct answer should be Harrisburg
What is the capital of Rhode Island?s
The correct answer should be Providence
What is the capital of South Carolina?s
The correct answer should be Columbia
What is the capital of South Dakota?s
The correct answer should be Pierre
What is the capital of Tennessee?s
The correct answer should be Nashville
What is the capital of Texas?s
The correct answer should be Austin
What is the capital of Utah?s
The correct answer should be Salt Lake City
What is the capital of Vermont?s
The correct answer should be Montpelier
What is the capital of Virginia?s
The correct answer should be Richmond
What is the capital of Washington?s
The correct answer should be Olympia
What is the capital of West Virginia?s
The correct answer should be Charleston
What is the capital of Wisconsin?s
The correct answer should be Madison
What is the capital of Wyoming?s
The correct answer should be Cheyenne
The correct count is 3
**8.37(Guess the capital)Write a program that repeatedly prompts the user for the capital of a state. After receiving user input, the program reports whether the answer is correct. They are stored in a two-dimensional array of 50 state capitals,. The program prompts the user to answer the capitals of all States and displays the number of correct answers (regardless of the case of the letters). Here is a running example.
What is the capital of Alabama?Montgomey
Your answer is correct
What is the capital of Alaska?Juneau
Your answer is correct
What is the capital of Arizona?Phoenix
Your answer is correct
What is the capital of Arkansas?q
The correct answer should be Little Rock
What is the capital of California?s
The correct answer should be Sacramento
What is the capital of Colorado?s
The correct answer should be Denver
What is the capital of Connecticut?s
The correct answer should be Hartford
What is the capital of Delaware?s
The correct answer should be Dover
What is the capital of Florida?s
The correct answer should be Tallahassee
What is the capital of Georgia?s
The correct answer should be Atlanta
What is the capital of Hawaii?s
The correct answer should be Honolulu
What is the capital of Idaho?s
The correct answer should be Boise
What is the capital of Illinois?s
The correct answer should be Springfield
What is the capital of Indiana?s
The correct answer should be Indianapolis
What is the capital of Iowa?s
The correct answer should be Des Moines
What is the capital of Kansas?s
The correct answer should be Topeka
What is the capital of Kentucky?s
The correct answer should be Frankfort
What is the capital of Louisiana?s
The correct answer should be Baton Rouge
What is the capital of Maine?s
The correct answer should be Augusta
What is the capital of Maryland?s
The correct answer should be Annapolis
What is the capital of Massachusettes?s
The correct answer should be Boston
What is the capital of Michigan?s
The correct answer should be Lansing
What is the capital of Minnesota?s
The correct answer should be Saint Paul
What is the capital of Mississippi?s
The correct answer should be Jackson
What is the capital of Missouri?s
The correct answer should be Jefferson City
What is the capital of Montana?s
The correct answer should be Helena
What is the capital of Nebraska?s
The correct answer should be Lincoln
What is the capital of Nevada?s
The correct answer should be Carson City
What is the capital of New Hampshire?s
The correct answer should be Concord
What is the capital of New Jersey?s
The correct answer should be Trenton
What is the capital of New York?s
The correct answer should be Albany
What is the capital of New Mexico?s
The correct answer should be Santa Fe
What is the capital of North Carolina?s
The correct answer should be Raleigh
What is the capital of North Dakota?s
The correct answer should be Bismark
What is the capital of Ohio?s
The correct answer should be Columbus
What is the capital of Oklahoma?s
The correct answer should be Oklahoma City
What is the capital of Oregon?s
The correct answer should be Salem
What is the capital of Pennslyvania?s
The correct answer should be Harrisburg
What is the capital of Rhode Island?s
The correct answer should be Providence
What is the capital of South Carolina?s
The correct answer should be Columbia
What is the capital of South Dakota?s
The correct answer should be Pierre
What is the capital of Tennessee?s
The correct answer should be Nashville
What is the capital of Texas?s
The correct answer should be Austin
What is the capital of Utah?s
The correct answer should be Salt Lake City
What is the capital of Vermont?s
The correct answer should be Montpelier
What is the capital of Virginia?s
The correct answer should be Richmond
What is the capital of Washington?s
The correct answer should be Olympia
What is the capital of West Virginia?s
The correct answer should be Charleston
What is the capital of Wisconsin?s
The correct answer should be Madison
What is the capital of Wyoming?s
The correct answer should be Cheyenne
The correct count is 3 -
参考代码:
package chapter08; import java.util.Scanner; public class Code_37 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String[][] stateCapital = {/*55个州和它们独自的首府*/ {"Alabama", "Montgomey"}, {"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", "Bismark"}, {"Ohio", "Columbus"}, {"Oklahoma", "Oklahoma City"}, {"Oregon", "Salem"}, {"Pennslyvania", "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"}}; int count=0; for (int i = 0; i < stateCapital.length; i++) { System.out.print("What is the capital of " + stateCapital[i][0] + "?"); String guessCapital = input.nextLine(); if (guessCapital.equals(stateCapital[i][1])) { System.out.println("Your answer is correct"); count++; } else System.out.println("The correct answer should be " + stateCapital[i][1]); } System.out.println("The correct count is " + count); } }
-
结果显示:
What is the capital of Alabama?Montgomey Your answer is correct What is the capital of Alaska?Juneau Your answer is correct What is the capital of Arizona?Phoenix Your answer is correct What is the capital of Arkansas?q The correct answer should be Little Rock What is the capital of California?s The correct answer should be Sacramento What is the capital of Colorado?s The correct answer should be Denver What is the capital of Connecticut?s The correct answer should be Hartford What is the capital of Delaware?s The correct answer should be Dover What is the capital of Florida?s The correct answer should be Tallahassee What is the capital of Georgia?s The correct answer should be Atlanta What is the capital of Hawaii?s The correct answer should be Honolulu What is the capital of Idaho?s The correct answer should be Boise What is the capital of Illinois?s The correct answer should be Springfield What is the capital of Indiana?s The correct answer should be Indianapolis What is the capital of Iowa?s The correct answer should be Des Moines What is the capital of Kansas?s The correct answer should be Topeka What is the capital of Kentucky?s The correct answer should be Frankfort What is the capital of Louisiana?s The correct answer should be Baton Rouge What is the capital of Maine?s The correct answer should be Augusta What is the capital of Maryland?s The correct answer should be Annapolis What is the capital of Massachusettes?s The correct answer should be Boston What is the capital of Michigan?s The correct answer should be Lansing What is the capital of Minnesota?s The correct answer should be Saint Paul What is the capital of Mississippi?s The correct answer should be Jackson What is the capital of Missouri?s The correct answer should be Jefferson City What is the capital of Montana?s The correct answer should be Helena What is the capital of Nebraska?s The correct answer should be Lincoln What is the capital of Nevada?s The correct answer should be Carson City What is the capital of New Hampshire?s The correct answer should be Concord What is the capital of New Jersey?s The correct answer should be Trenton What is the capital of New York?s The correct answer should be Albany What is the capital of New Mexico?s The correct answer should be Santa Fe What is the capital of North Carolina?s The correct answer should be Raleigh What is the capital of North Dakota?s The correct answer should be Bismark What is the capital of Ohio?s The correct answer should be Columbus What is the capital of Oklahoma?s The correct answer should be Oklahoma City What is the capital of Oregon?s The correct answer should be Salem What is the capital of Pennslyvania?s The correct answer should be Harrisburg What is the capital of Rhode Island?s The correct answer should be Providence What is the capital of South Carolina?s The correct answer should be Columbia What is the capital of South Dakota?s The correct answer should be Pierre What is the capital of Tennessee?s The correct answer should be Nashville What is the capital of Texas?s The correct answer should be Austin What is the capital of Utah?s The correct answer should be Salt Lake City What is the capital of Vermont?s The correct answer should be Montpelier What is the capital of Virginia?s The correct answer should be Richmond What is the capital of Washington?s The correct answer should be Olympia What is the capital of West Virginia?s The correct answer should be Charleston What is the capital of Wisconsin?s The correct answer should be Madison What is the capital of Wyoming?s The correct answer should be Cheyenne The correct count is 3 Process finished with exit code 0