【问题标题】:Why won't the method return true when conditions are met?为什么满足条件时方法不会返回true?
【发布时间】:2017-11-25 19:03:40
【问题描述】:

我正在使用 GUI 在 Netbeans 中编写游戏。在游戏中,随机产生15-30之间的石头,用户和计算机轮流去除1-3之间的石头,直到没有剩余。最后拿走最后一块石头的玩家输了。我的问题是我的方法(validIn())不会在playerMove()中使用,所以即使满足条件,游戏也不会更新。 这是我认为问题所在:

private boolean validIn(int num){
    //
    return num < 3 || num > 1 || num < totalStone;
}

public void playerMove(){
    // Geting the user input
    int userIn = Integer.parseInt(txtIn.getText());
    // Declaring and assigning a boolean
    boolean check = false;

    // If the boolean returns true
    if (check == true) {
        // Subtracting how much the player took from the total amount of rocks
        totalStone -= userIn;
        // Displaying how many rocks were taken and how many are left
        txtOut.setText(txtOut.getText() +"\nYou picked up " +userIn +" stone(s). There are " +totalStone +" stone(s) left.");                
    }
        // Else,
        else {
            // Assign the boolean check to what the method validIn returns 
            check = validIn(userIn);
        }

}

public void checkWinner(String player){
    // If there are no more stones left,
    if (totalStone == 0){                              
    // Display a message that says who won
    txtOut.setText(txtOut.getText() +"\nThere are no more stones left. "+player +" wins!");
   }  
}

private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //
    if (totalStone > 0){
        // Display how many rocks there are
        txtOut.setText(txtOut.getText() +"\nThere are " +totalStone +" stone(s).");
        // The player does their move
        playerMove();
        // Checking if the computer won
        checkWinner("The computer");
        // Computer does a turn
        computerMove();
        // Checking if the player won
        checkWinner("The player");            
    }

        //
        else if (totalStone == 0){
            //
            txtOut.setText(txtOut.getText() + "\nThe game has ended.");
        }
}                                        

【问题讨论】:

  • 当你声明为假之前的那一行时,如何检查为真?
  • 哪种方法不会返回 true?您发布的唯一返回 boolean 的方法将总是返回 true。
  • 另外... validIn() 将始终返回 true,因为每个整数要么小于 3,要么大于 1

标签: java methods netbeans boolean user-input


【解决方案1】:
  1. int comIn = (int)(Math.random() * 2) + 1;
    // Declaring and assigning a boolean boolean check = false; // If number generated is bigger than the total stones, if (check == true && validIn(userIn) == true){...}

在上面的代码中,您将生成的数字保存在 comIn 中,但您从不检查 comeIn 是否大于代码中的总石头。

然后在 if 条件下,您检查的是 check 是否等于 boolean true,这不是因为您在上面给了 check 一个 false 值并且从未更改过它,因此它永远不会运行 if 循环,因为在其中第一个条件是错误的.

  1. private boolean validIn(int num){ return num < 3 || num > 1 || num < totalStone; }

这里无论如何都会返回 true,因为如果您按照自己的规则玩,提供的 num 始终小于 3 且大于 1

  1. `check = false;

    // 如果布尔值返回 true if (check == true) {...} `

在这里,您再次设置 check false 的值并在 if 循环中检查它是否为 true,这将永远不会运行,因为 check 永远不会为 true。

【讨论】:

    【解决方案2】:

    对于您的 validIn 方法,您需要考虑一下您的方法返回语句的条件。

    写法:

    • 如果 num 是小于 3 的任意值,例如 0、-50、1、2,则返回 true 如果不是,则
    • 如果 num 是任何大于 1 的值,例如 2、3、1020、323243,则返回 true 如果不是,则
    • 如果 num 小于 totalStones 则返回 true 如果不是则返回 false。

    永远不会达到检查 num 是否小于 totalStones 的条件,因为您使用的是 OR (||) 运算符。通过使用第一个和第二个条件,您将始终返回 true。如果发现第一个条件 (num ) 为真,则立即返回真,但如果不是,则检查第二个条件 (num > 1),如果发现它为真,然后立即返回真。第三个条件 (num ) 就是无法达到。

    您要做的是将 || 运算符更改为 AND (&&) 运算符:

    return num < 3 && num > 1 && num < totalStone;
    
    • 仅当 num 小于 3num 大于 1 时返回 true num 小于 totalStones

    通过使用 && 运算符,您可以确保在返回 true 之前必须满足所有三个条件。现在这样说,您可以看到只有 num 中的 2 值会返回 true。我认为你的意思是:

    return num <= 3 && num >= 1 && num < totalStone;
    

    这样,如果 num 拥有 1、2 或 3 并且其中任何一个值小于 totalStones,则返回 true,否则返回 false。

    【讨论】:

      【解决方案3】:

      在您的代码中,我看到 boolean 方法总是返回 true,因为您使用了

      || (OR)
      

      任何小于 3 的数字都将返回 true,或者大于 1 的数字也将返回 true。

      在您的 IF 条件下,我没有看到您将变量 check 设置为 true 但您仍然有条件 checkTRUE 因此我想知道如何使用此变量来检查您的条件? 那么如何跳转到 if 语句中呢?

      【讨论】:

        【解决方案4】:

        由于某种原因,playerMove() 方法中的 validIn(userIn) 永远无法到达。我必须删除布尔检查并更改 if (check == true)if ((validIn(userIn) == true)

        【讨论】:

          猜你喜欢
          • 2021-05-03
          • 2021-04-22
          • 1970-01-01
          • 2018-04-13
          • 1970-01-01
          • 1970-01-01
          • 2012-06-20
          • 2013-07-08
          • 2018-09-07
          相关资源
          最近更新 更多