我正在开发一个抛硬币程序,并希望允许用户使用toUpperCase()输入小写或大写的尾部/尾部。但由于某种原因,在我输入任何方式前后,它将跳过循环的整个过程并打印最后一个语句。我不知道为什么,因为我有类似的程序,它工作得很好。感谢您的建议!
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++;
答案 0 :(得分:2)
使用equalsIgnoreCase
代替equals
。然后你不必考虑大/小写(你已经混淆了,顺便说一下)