java - to.UpperCase 跳过它应该工作的过程

标签 java loops uppercase

我正在开发一个抛硬币程序,希望允许用户使用 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/

相关文章:

java - Android TextView 没有完全更新

JavaFX:LoadException 解析按钮事件处理程序的 onAction

java - 使用 java 、Redis、Elastic Search 、Mongo 自动完成

Java TCP 客户端/服务器和循环问题

Python循环迭代问题

Windows批处理文件读取文本文件并全部转换为大写

javascript - 避免区分大小写字符的优雅方法? (最好在 JavaScript 中)

java - 更改java中字符的小写和大写

java - 如何通过 POST (Ajax) 发送 JSON 数据并从 Struts 2 操作接收 JSON 响应

python - 如何优雅地使用 Python 迭代器