我正在开发一个抛硬币程序,希望允许用户使用 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++;
最佳答案
使用equalsIgnoreCase
而不是equals
。然后你就不必考虑大写/小写(顺便说一句,你混淆了)
关于java - to.UpperCase 跳过它应该工作的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46721413/