to.UpperCase跳过它应该工作的过程
问题描述:
我正在研究一个硬币折腾程序,并希望允许用户使用toUpperCase()输入小写或大写头部/尾部。但由于某种原因,在我输入正面或反面之后,它会跳过循环的整个过程并打印最后一个语句。我不知道为什么,因为我有类似的程序,它工作得很好。感谢您的建议!to.UpperCase跳过它应该工作的过程
do {
System.out.printf("Do you predict heads or tails for the coin toss?(heads or tails): ");
String predict = in.next();
System.out.print("Tossing a coin...");
System.out.println();
predict = predict.toUpperCase();
int cointoss= (int) (Math.random()*OUTCOMES);
if(predict.equals("heads") && cointoss==0){
System.out.print("The coin came up heads. You win!");
System.out.println();
wins ++;
}
System.out.print("Type \"c\" to continue or any other letter to quit: ");
playAgain = in.next();
if (playAgain.equals("c")){
done = false;
} else done = true;
count++;
答
使用equalsIgnoreCase
而不是equals
。那么你不必考虑大/小写(你已经混在一起,顺便说一句)
嗯,你把它转换为大写,至少把它与小写比较。由于没有包含全部内容,因此没有关于循环的线索,但要确保字符串正是您认为的那样。 –
尝试打印变量或使用调试程序,这将有助于您快速解决此问题 – rpfun12
如果您使用'“HEADS”'代替'“heads”',会发生什么? –