无法摆脱输入中的空格
Can't get rid of spaces in input
我正在尝试编写州和首都测验的代码,我几乎完成了它,但是当我尝试输入任何我必须输入 2 个单词的首都(即小石城或俄克拉荷马城)时,它有它作为 2 个独立的输入,在我的一生中,我无法将它算作一个。
到目前为止,这是我的代码:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
ArrayList<String> States = new ArrayList<>();
ArrayList<Object> Capitals = new ArrayList<>(States);
String capitals [][] = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "LittleRock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"Georgia", "Atlanta"},
{"Hawaii", "Honolulu"},
{"Idaho", "Boise"},
{"Illinois", "Springfield"},
{"Indiana", "Indianapolis"},
{"Iowa", "DesMoines"},
{"Kansas", "Topeka"},
{"Kentucky", "Frankfort"},
{"Louisiana", "BatonRouge"},
{"Maine", "Augusta"},
{"Maryland", "Annapolis"},
{"Massachusettes", "Boston"},
{"Michigan", "Lansing"},
{"Minnesota", "Saint Paul"},
{"Mississippi", "Jackson"},
{"Missouri", "JeffersonCity"},
{"Montana", "Helena"},
{"Nebraska", "Lincoln"},
{"Nevada", "CarsonCity"},
{"New Hampshire", "Concord"},
{"New Jersey", "Trenton"},
{"New York", "Albany"},
{"New Mexico", "SantaFe"},
{"North Carolina", "Raleigh"},
{"North Dakota", "Bismark"},
{"Ohio", "Columbus"},
{"Oklahoma", "OklahomaCity"},
{"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"}
};
for(int i = 0; i<=49; i++){
States.add(capitals[i][0]);
Capitals.add(capitals[i][1]);
}
for(int i = 0; i<49; i++){
String guess;
int index = (int)((Math.random())*(49 - i));
System.out.println("What is the capital of: " + States.get(index));
States.remove(index);
guess = input.next();
if(Capitals.get(index).equals(guess)){
System.out.println("Correct");
Capitals.remove(index);
}else{ System.out.println("Incorrect");
Capitals.remove(index);
}
}
}
详细说明@Gus 在这里所说的内容
" 您可以更改定界符模式,也可以使用 nextLine()",
阅读单个标记可能是您的失败。 "I like pie" 读作 "I" "like" "pie" with line.next() 但一起读作 "I like pie" with line.next行()
编辑更多细节
引入示例行:
for(int i = 0; i<=49; i++) // {"Utah", "Salt Lake City"},
{
States.add(capitals[i][0]); // add Utah
Capitals.add(capitals[i][1]); // add Salt
}
在这个索引中,capitals[44][],大小设置为 4,因为每个单词都被读取。
我正在尝试编写州和首都测验的代码,我几乎完成了它,但是当我尝试输入任何我必须输入 2 个单词的首都(即小石城或俄克拉荷马城)时,它有它作为 2 个独立的输入,在我的一生中,我无法将它算作一个。
到目前为止,这是我的代码:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
ArrayList<String> States = new ArrayList<>();
ArrayList<Object> Capitals = new ArrayList<>(States);
String capitals [][] = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "LittleRock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
{"Connecticut", "Hartford"},
{"Delaware", "Dover"},
{"Florida", "Tallahasse"},
{"Georgia", "Atlanta"},
{"Hawaii", "Honolulu"},
{"Idaho", "Boise"},
{"Illinois", "Springfield"},
{"Indiana", "Indianapolis"},
{"Iowa", "DesMoines"},
{"Kansas", "Topeka"},
{"Kentucky", "Frankfort"},
{"Louisiana", "BatonRouge"},
{"Maine", "Augusta"},
{"Maryland", "Annapolis"},
{"Massachusettes", "Boston"},
{"Michigan", "Lansing"},
{"Minnesota", "Saint Paul"},
{"Mississippi", "Jackson"},
{"Missouri", "JeffersonCity"},
{"Montana", "Helena"},
{"Nebraska", "Lincoln"},
{"Nevada", "CarsonCity"},
{"New Hampshire", "Concord"},
{"New Jersey", "Trenton"},
{"New York", "Albany"},
{"New Mexico", "SantaFe"},
{"North Carolina", "Raleigh"},
{"North Dakota", "Bismark"},
{"Ohio", "Columbus"},
{"Oklahoma", "OklahomaCity"},
{"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"}
};
for(int i = 0; i<=49; i++){
States.add(capitals[i][0]);
Capitals.add(capitals[i][1]);
}
for(int i = 0; i<49; i++){
String guess;
int index = (int)((Math.random())*(49 - i));
System.out.println("What is the capital of: " + States.get(index));
States.remove(index);
guess = input.next();
if(Capitals.get(index).equals(guess)){
System.out.println("Correct");
Capitals.remove(index);
}else{ System.out.println("Incorrect");
Capitals.remove(index);
}
}
}
详细说明@Gus 在这里所说的内容 " 您可以更改定界符模式,也可以使用 nextLine()",
阅读单个标记可能是您的失败。 "I like pie" 读作 "I" "like" "pie" with line.next() 但一起读作 "I like pie" with line.next行()
编辑更多细节 引入示例行:
for(int i = 0; i<=49; i++) // {"Utah", "Salt Lake City"},
{
States.add(capitals[i][0]); // add Utah
Capitals.add(capitals[i][1]); // add Salt
}
在这个索引中,capitals[44][],大小设置为 4,因为每个单词都被读取。