【发布时间】:2016-04-29 01:22:19
【问题描述】:
以下是我在 Java 中遇到问题的一段代码;它似乎已将一个问题换成了另一个问题。由于空格错误,当它是一个句子时,我在检索用户输入时遇到问题;我通过添加“nextLine”而不是“next”来解决这个问题。
但是现在,它没有给用户回答第一个问题的机会,而是跳过控制台框并简单地输出“错误”,然后继续第二个问题,此时用户的第一个控制台框出现了。
我仔细检查了:使用“next”,显示第一个问题的控制台,但使用“nextLine”,它跳过该控制台框(不给用户输入任何内容的机会),吐出“错误”,然后转到问题二,用户终于可以输入一些东西了。
为什么要这样做?
if(nwb.equalsIgnoreCase("numbers")){
System.out.println("You must type in the rules verbatim "+
"(word for word, including punctuation), "+
"as shown on the Student Hand-Out. "+
"Capitalization does not matter.");
int count = 1;
for(int i = 0; i<=(rules.length-1); i++){
System.out.print("Rule #"+count);
String response = console.nextLine().replaceAll(" ","");
if(response.replaceAll(" ", "").equalsIgnoreCase(rules[i].replaceAll(" ", ""))){
System.out.println("CORRECT");
}else{
System.out.println("WRONG\nThe correct wording is: "+rules[i]);
}
count++;
}
}
【问题讨论】:
-
高亮显示代码并按 ctrl+k。
-
啊!这就是它的完成方式。谢谢,对此感到抱歉。
-
rules 是代码前面的字符串数组的名称。