下面是我在Java中遇到问题的一段代码;它似乎已经为另一个交易了一个问题。由于空白错误,我在检索用户输入时遇到问题;我通过添加“nextLine”而不是“next”来解决这个问题。在Java中第一次跳过控制台?
但是现在,它不会让用户有机会回答第一个问题,它会跳过控制台框并在继续问题2之前输出“错误”,现在用户的第一个控制台框显示出来。
我仔细检查:用“下一个”,第一个问题的控制台出现,但用“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。 – CConard96
啊!这是如何完成的。谢谢,并对此表示歉意。 – Ray25Lee
什么是规则? – Priyamal