我摘录了需要处理的代码,我知道发布所有代码的标准做法,但是我认为这里不需要。
if(playerValue > 10 && playerValue < 21){
System.out.println("Players Value so far " + playerValue + ", Do you want to draw another card? Y/N");
// input a y or n answer
decision = sob.next();
if(decision.equals("Y") || decision.equals("y")){
continue;
}else if(decision.equals("N") || decision.equals("n")){
break;
}
}
}
我如何更改此语句以接受Y或N,就像现在,如果我在两个字符之间输入任何东西,它们将像按Y一样继续。
最佳答案
您应该有其他情况:
if(playerValue > 10 && playerValue < 21){
System.out.println("Players Value so far " + playerValue + ", Do you want to draw another card? Y/N");
// input a y or n answer
decision = sob.next();
if(decision.equalsIgnoreCase("Y")){
continue;
}else if(decision.equalsIgnoreCase("N")){
break;
}
else
{
//whatever you want to happen if they don't enter either y or n
}
}
}
也许在else中有另一个循环,说请输入一个有效的输入并保持循环,直到他们给出一个有效的输入为止。