to.UpperCase 跳过它应该工作的过程
to.UpperCase skips the process where it should work
我正在开发一个抛硬币程序,希望允许用户使用 toUpperCase() 输入小写或大写 heads/tails。但出于某种原因,在我输入正面或反面后,它将跳过循环的整个过程并打印最后一条语句。我不确定为什么,因为我有类似的程序并且运行良好。感谢您的建议!
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
。那么你不必考虑 upper/lower 案例(你搞混了,顺便说一句)
我正在开发一个抛硬币程序,希望允许用户使用 toUpperCase() 输入小写或大写 heads/tails。但出于某种原因,在我输入正面或反面后,它将跳过循环的整个过程并打印最后一条语句。我不确定为什么,因为我有类似的程序并且运行良好。感谢您的建议!
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
。那么你不必考虑 upper/lower 案例(你搞混了,顺便说一句)