当if语句中的条件不满足时,为什么在我的while循环中执行else语句?

时间:2017-11-18 23:47:25

标签: java loops if-statement methods while-loop

我制作了一款名为“Nim'游戏”的游戏。在Netbeans。基本上,产生15-30之间的随机数量的宝石,并且计算机和玩家轮流采取1-3个宝石,直到没有留下。拿最后一块石头的玩家输了。我以jframe形式对此进行编码。我想确保玩家不输入大于3,小于1且大于总石头数的数字,所以我做了一个带有if语句的while循环,用于符合要求的输入和else语句如果他们没有遇到。我的问题是,当玩家确实输入了不应输入的数字时,不会显示任何错误消息,游戏将继续正常进行。 这是我认为问题所在:

    public int playerInput(){
        // Getting the user input and converting it into an integer
        int userIn = Integer.parseInt(txtIn.getText());
        //
        boolean input = false;

        // Do this while the input isn't between 1-3 and higher than the total amount of rocks
        while(!input){
            //
            if (userIn < 3 || userIn > 1 || userIn < totalStone){
                //
                input = true;                                      
            }
                //
                else{
                    // Output an error message
                    txtaOut.setText(txtaOut.getText() +"\nEnter a number between 1 - 3 and less than the amount of stones left.");
                }   
        }
        // return the amount of rocks the user takes
        return userIn;              
}

以下是游戏的大部分代码(我将使用随机斜杠进行评论):

    public void computerMove() {      
    // Generating a number for the computer's move
    int comIn = (int)(Math.random() * 2) + 1;            

    // If number generated is bigger than the total stones,
    if (comIn > totalStone){           
        // Get the difference between the total and the random number
        int totalComDiff = Math.abs(totalStone - comIn);
        // Subtract the difference from the random number
        comIn -= totalComDiff;
        // Substract the rocks taken from the total
        totalStone -= comIn;
        // Display a message of the rocks taken and the rocks left
        txtaOut.setText(txtaOut.getText() +"\nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");
        }
            // Otherwise, if the random number is smaller than the total,
            else if (comIn < totalStone){
                // Substract the rocks taken from the total
                totalStone -= comIn;
                // Display a message of the rocks taken and the rocks left
                txtaOut.setText(txtaOut.getText() +"\nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");

            }
             // If the total equals amount the computer takes,
            else if (totalStone == comIn){                    
                // Substract the rocks taken from the total
                totalStone -= comIn;
                // Display a message of the rocks taken and the rocks left
                txtaOut.setText(txtaOut.getText() +"\nThe computer picked up " +comIn +" stone(s). There are " +totalStone +" stones left.");          
                // Display a message that says the player wins
                txtaOut.setText(txtaOut.getText() +"\nThere are no more stones left. The player wins!");
            }            

            // Otherwise, if the amount of stones is 0,
            else if (totalStone == 0){
                // Display an end game message
                txtaOut.setText(txtaOut.getText() + "\nThe game has ended.");
            }

}
public void playerMove(){
    // If there are no more stones left,
    if (playerInput() == totalStone){
        // Subtracting how much the player took from the total amount of rocks
        totalStone -= playerInput();
        // Displaying how many rocks were taken and how many are left
        txtaOut.setText(txtaOut.getText() +"\nYou picked up " +playerInput() +" stone(s). There are " +totalStone +" stones left.");                

        // Display a message that says the computer wins
        txtaOut.setText(txtaOut.getText() + "\nThere are no more stones left. The computer wins.");
    }
        //
        else if (playerInput() != totalStone){
            // Subtracting how much the player took from the total amount of rocks
            totalStone -= playerInput();
            // Displaying how many rocks were taken and how many are left
            txtaOut.setText(txtaOut.getText() +"\nYou picked up " +playerInput() +" stone(s). There are " +totalStone +" stones left.");                

        }
        //
        else if (totalStone <= 0){
            //
            txtaOut.setText(txtaOut.getText() + "\nThe game has ended.");
            }        
}
    private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //
    if (totalStone > 0){
        // Display how many rocks there are
        txtaOut.setText(txtaOut.getText() +"\nThere are " +totalStone +" stones.");
        // The player does their move
        playerMove();


    }

    //
    if (totalStone > 0){
        // Computer does a turn
        computerMove();
    }

        //
        else if (totalStone == 0){
            //
            txtaOut.setText(txtaOut.getText() + "\nThe game has ended.");
        }
}                                        
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // Generating another random number
    totalStone = (int)(Math.random() * 15) + 15;

    // Clearing all the textfields
    txtIn.setText("");
    txtaOut.setText("");

    // Outputting the number of starting stones
    txtaOut.setText(txtaOut.getText() +"There are " +totalStone +" stones. It's your turn.");

} 

2 个答案:

答案 0 :(得分:0)

你需要看起来像这样:

2012
a :  2
b :  2
2013
a :  1
b :  0

而且,它会起作用。

最好首先检查条件是否无效,然后如果验证通过则执行else块中的正常流程。

你编写它的方式,因为||,if条件总是正确的代表'或'所以你要求天气userIn小于3或大于1或小于totalStone总是如此。

另一方面&amp;&amp;代表'和'和'!'代表不。因此,您基本上希望满足所有条件,并检查它们是否不是通过将它们放入括号并放置! (否定)在前面

如果不满足条件,您还需要再次提示用户。否则它将永远运行并冻结ui。

答案 1 :(得分:0)

如果用户输入的号码不正确,则应提示他们输入新号码。在你的代码中,它将永远停留在循环中。

您还应该使用&amp;&amp;来检查是否满足所有条件。 您需要一个(&lt; = 3)AND(&gt; = 1)AND(&lt; = totalStones)

的数字
public int playerInput () {

    int userIn;

    do {

        System.out.println("Enter a number between 1 - 3 and less than the amount of stones left.")

        userIn = Integer.parseInt (txtIn.getText ());

    } while (!(userIn <= 3 && userIn >= 1 && userIn <= totalStone));

在不满足条件的情况下,这将继续循环。

相关问题