【问题标题】:If statement after default switch case - Java默认switch case之后的if语句-Java
【发布时间】:2013-10-31 05:48:09
【问题描述】:

我正在开发一个 Java 程序,并且我正在努力为我的程序创建我所说的统计区域。我创建了一个“摇滚”“纸”“剪刀”程序,我试图告诉用户他们赢了多少次,计算机赢了多少次,最后,有多少平局。

请原谅我的无知,我正在学习。这是我的代码:

 import java.util.Scanner;

    public class TheAntlers {
    public static void main(String[] args) {

        int playerHumanWins = 0;
        int playerComputerWins = 0;
        int numberOfTies = 0;
        int computerResult;

        Scanner input = new Scanner(System.in);

        while(true) {

            String startGame;
            String playerHuman;
            String playerComputer = " ";

            System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
            startGame = input.nextLine().toUpperCase();

            if(startGame.equals("N")) {
                System.out.println("NO!");
                break;
            }
            else if(! startGame.equals("Y")) {
                startGame = startGame.toLowerCase();
                System.out.println("Sorry, " + startGame + " is not a valid entry...");               
            }
            while(startGame.equals("Y")) {
                System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
                playerHuman = input.nextLine().toUpperCase();

                computerResult = (int)(Math.random() * 3);

                if(computerResult == 0) {
                    playerComputer = "ROCK";
                }
                else if(computerResult == 1) {
                    playerComputer = "PAPER";
                }
                else if (computerResult == 2) {
                    playerComputer = "SCISSORS";
                }

                switch (playerHuman) {
                    case "ROCK" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"ROCK\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("Sorry, the computer wins!");
                            playerComputerWins++;
                        }
                        else {
                            System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        break;
                    case "PAPER" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"PAPER\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("ROCK")) {
                            System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    case "SCISSORS" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"SCISSORS\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    default:
                        playerHuman = playerHuman.toLowerCase();
                        System.out.println("Sorry, " + playerHuman + " is not a valid entry...");      
                }

                if(playerHumanWins == 1) {
                    System.out.println("You won: " + playerHumanWins + " times");
                    System.out.println("The computer won " + playerComputerWins + " times");
                    System.out.println("There were " + numberOfTies++);
                }
            }
        }
    }
}

基本上我已经做了一个 if 语句,当用户至少获胜一次时,显示统计信息但这似乎不起作用。我不能 100% 确定如何显示统计信息。

【问题讨论】:

  • return 声明对你没有帮助
  • @MadProgrammer 怎么来的?

标签: java if-statement printing while-loop switch-statement


【解决方案1】:

基本上,您的代码中的 return 语句会强制 Java 退出您的 (main) 方法,这会阻止它执行代码的“统计”部分并终止您的程序。

首先删除它们。

还有……

if (playerHumanWins == 1) {...

意味着统计数据只会显示一次,当玩家第一次获胜时。不确定这是否是你真正想要的......

更新

基于游戏只能在玩家获胜之前结束的条件,您可以执行类似...

    if (playerHumanWins == 1) {
        break;
    }
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

或者……

while (startGame.equalsIgnoreCase("Y") && playerHumanWins < 1) {
    //...
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

无论哪种方式,您还应该摆脱 while (true) { 语句,它增加了不必要的复杂性...

【讨论】:

  • 一旦用户获胜,程序就会终止。因此,在他们获胜之前,他们可能会先输给计算机 9 次和 12 次平局,所以最后应该说计算机 - 12 次平局 - 9 次和胜利 - 1 次等等。
  • 那么,停止比赛的唯一方法就是赢球?
  • 是的,停止比赛的唯一方法就是赢得比赛。
  • 感谢您抽出宝贵时间帮助我。你会建议什么而不是 while(true)?
  • 我不会。把它放在外面。一旦你离开(当前)第二个循环,你应该退出游戏......?您可以更改逻辑以改用do-while...
【解决方案2】:

您正在使用return 跳出交换机。 Return 退出该方法。不如改用break

还要确保在玩新游戏之前将 playerHumanWins 和其他人的值重置为零

【讨论】:

  • 我会先检查代码的逻辑。我能找到的所有return 语句都在if-else 块内...
【解决方案3】:

修改部分改动如下:

来自

 else if(! startGame.equals("Y")) {
     startGame = startGame.**toLowerCase()**;
     System.out.println("Sorry, " + startGame + " is not a valid entry...");               
 }

else if(! startGame.equals("Y")) {
    startGame = startGame.**toUpperCase()**;
    System.out.println("Sorry, " + startGame + " is not a valid entry...");               
}

【讨论】:

  • 此更改仅影响用户输入无效选择时回显输出的情况 - 与问题完全无关
猜你喜欢
  • 2016-05-15
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多