Capital/State 猜谜游戏二维数组
Capital/State Guessing Game 2D Arrays
我一直在网上查找并找到一些能够完成此类代码的人,但没有人为我直接看到的问题提供解决方案。这里的任务是让程序询问用户"What is the capital of Arizona"。用户需要输入诸如 Phoenix 之类的大写字母,它会奖励一个点并一直移动到下一个问题,同时计算正确答案的数量并在完成后将其显示在输出的底部。当 运行 我的程序询问列表中第一个州的首都时。一旦完成,它将提供一个点,但随后吐出其余状态,说它们是正确的,但不给用户进入下一个状态的机会。就好像我错过了当第一个状态被回答时会回到 "Enter the capital of the state listed" 的原始问题的部分。有人可以提供帮助吗?我相信我需要一个类似 if、else if 和 else 的语句,但看不出我将如何在此处应用它,或者我是否应该尝试另一个循环。我无法让 do 循环正常工作。有什么建议么?我的代码如下。
import java.util.Scanner;
public class StatesArray {
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) {
int correctStates = 0;
String userInput = "";
int i = 0;
final int NUMBER_OF_US_STATES = 49;
String capitals [][] = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"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"}
};
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
i = 0;
for(i = 0; i <= NUMBER_OF_US_STATES; i++){
if(capitals[i][1].equals(userInput)) {
correctStates++;
System.out.println("Yes that is the capital of " + capitals[i][0] + ".");
}
else {
System.out.println("No that is not the capital of " + capitals[i][0] + ".");
i++;
}
}
//counts the number of correct guesses.
System.out.println("Total number of capitals answered correctly: " + correctStates + ".");
i++;
return;
}
}
您必须将问题和输入移动到 for 循环内,您只要求输入一次,如果正确则打印所有正确,否则以其他方式打印。下面两行应该在 for 循环内。
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
而且我认为有 50 个州(不是 49 个)?此外,如果您更改为 50,您的条件应该是 < NUMBER_OF_STATES
而不是 <=,否则您将有一个 ArrayIndexOutOfBounds。
我一直在网上查找并找到一些能够完成此类代码的人,但没有人为我直接看到的问题提供解决方案。这里的任务是让程序询问用户"What is the capital of Arizona"。用户需要输入诸如 Phoenix 之类的大写字母,它会奖励一个点并一直移动到下一个问题,同时计算正确答案的数量并在完成后将其显示在输出的底部。当 运行 我的程序询问列表中第一个州的首都时。一旦完成,它将提供一个点,但随后吐出其余状态,说它们是正确的,但不给用户进入下一个状态的机会。就好像我错过了当第一个状态被回答时会回到 "Enter the capital of the state listed" 的原始问题的部分。有人可以提供帮助吗?我相信我需要一个类似 if、else if 和 else 的语句,但看不出我将如何在此处应用它,或者我是否应该尝试另一个循环。我无法让 do 循环正常工作。有什么建议么?我的代码如下。
import java.util.Scanner;
public class StatesArray {
static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) {
int correctStates = 0;
String userInput = "";
int i = 0;
final int NUMBER_OF_US_STATES = 49;
String capitals [][] = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"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"}
};
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
i = 0;
for(i = 0; i <= NUMBER_OF_US_STATES; i++){
if(capitals[i][1].equals(userInput)) {
correctStates++;
System.out.println("Yes that is the capital of " + capitals[i][0] + ".");
}
else {
System.out.println("No that is not the capital of " + capitals[i][0] + ".");
i++;
}
}
//counts the number of correct guesses.
System.out.println("Total number of capitals answered correctly: " + correctStates + ".");
i++;
return;
}
}
您必须将问题和输入移动到 for 循环内,您只要求输入一次,如果正确则打印所有正确,否则以其他方式打印。下面两行应该在 for 循环内。
System.out.println("Enter the Capital of the State listed: " + capitals[i][0]);
userInput = scnr.next();
而且我认为有 50 个州(不是 49 个)?此外,如果您更改为 50,您的条件应该是 < NUMBER_OF_STATES
而不是 <=,否则您将有一个 ArrayIndexOutOfBounds。