首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么当if语句中的条件不满足时,while循环中的else语句不会执行?

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

Stack Overflow用户
提问于 2017-11-19 07:47:25
回答 2查看 81关注 0票数 0

我正在用Netbeans制作一个叫做“Nim的游戏”的游戏。基本上,随机生成15-30个之间的石块,计算机和玩家轮流取1-3个石块,直到没有剩下为止。拿到最后一块石头的玩家输了。我以jframe的形式对其进行编码。我想确保玩家输入的数字不会大于3,小于1,并且大于所有的棋子总数,所以我创建了一个while循环,其中if语句用于输入,满足要求,如果不满足,则使用else语句。我的问题是,当玩家输入了不应该输入的数字时,不会出现错误消息,游戏会照常进行。以下是我认为问题所在:

代码语言:javascript
复制
    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;              
}

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

代码语言:javascript
复制
    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.");

} 
EN

回答 2

Stack Overflow用户

发布于 2017-11-19 08:01:08

你的if需要看起来像这样:

代码语言:javascript
复制
while (!input) {
    if (!(userIn < 3 && userIn > 1 && userIn < totalStone)) {
       // Output an error message
       txtaOut.setText(txtaOut.getText() +"\nEnter a number between 1 - 3 and less than the amount of stones left.");
       // Prompt the user again
       userIn = Integer.parseInt(txtIn.getText());
    } else {
       input = true;
    }
}

而且,它会起作用的。

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

按照你写的方式,if条件总是为真,因为||代表' or‘,所以你会问userIn是否小于3或者大于1或者小于totalStone,这总是真的。

另一方面,&&代表' and‘和'!’表示不是。所以,你基本上希望所有的条件都得到满足,并通过将它们放入括号中并放置它们来检查它们是否满足!(否定)在前面

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

票数 0
EN

Stack Overflow用户

发布于 2017-11-19 18:39:40

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

您还应该使用&&检查是否满足所有条件。您需要一个数字(<=3)和(>=1)和(<=totalStones)

代码语言:javascript
复制
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));

当条件不满足时,这将继续循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47372316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档