【问题标题】:While Loop Not Terminating With Boolean Value ChangingWhile 循环没有随着布尔值的变化而终止
【发布时间】:2015-06-27 21:07:46
【问题描述】:

一个可行的例子是有一个 while 循环继续从用户那里获取一个数字,直到他们猜对了数字,然后它将返回 true 并退出 while 循环。

但是,如果我创建一个返回变量是真还是假的方法,则 while 循环不会终止。但是,如果 IF 条件在 while 循环内,它会终止并按预期工作。

按预期工作的代码:

 private void print() { // Method name not decided yet
        boolean playerGuessedCorrectly = false;
        while(!playerGuessedCorrectly) {
             [...] // Other code that asks the user to enter a new number
             // userGuess is the next int entered by the user
            // computerNumber is a random number between 1 and 100
             if(userGuess == computerNumber) {
                      System.out.print("You have guessed my number.");
                      playerGuessedCorrectly = true; 
              } // while loop terminates upon true
        } 
}

此代码有效,当它更改为 true 时,while 循环停止工作。但是,当我将相同的代码放在一个方法中时,while 循环会继续循环并且不会终止:

不工作的代码:

private void print() {
        boolean playerGuessedCorrectly = false;
        while(!playerGuessedCorrectly) {
              checkIfGuessIsMyNumber(playerGuessedCorrectly);
        } // value of playerGuessedCorrectly is sent to method checkIfGuessIsMyNumber();

private boolean checkIfGuessIsMyNumber(boolean playerGuessedCorrectly) {
    // userGuess is the next int entered by the user
    // computerNumber is a random number between 1 and 100
    if(userGuess == computerNumber) {
        System.out.print("You have guessed my number.");
        return playerGuessedCorrectly = true;
    } else {
        playerGuessedCorrectly = false;
    }
}

为了澄清,当 IF userGuess == computerNumber 在 while 循环内时,while 循环终止,但是,当它被拆分为不同的方法并返回值时,它不起作用。

请注意,当我打印出方法给出的值时,它已打印出 true。所以我很困惑为什么当方法中的值为真时它不会终止,但它会在 while 循环内的 if 条件下终止。

【问题讨论】:

    标签: java


    【解决方案1】:

    这一行:

    playerGuessedCorrectly = false;
    

    还有这一行:

    return playerGuessedCorrectly = true;
    

    不起作用,因为 Java 按值传递参数。您实际上是将false 分配给checkIfGuessIsMyNumber 函数的本地playerGuessedCorrectly 副本,不是 分配给print 函数中的原始版本。

    逐行跟随不工作的代码:

    boolean playerGuessedCorrectly = false;
    

    这按预期工作。

    while(!playerGuessedCorrectly) {
        // ...
    }
    

    如果playerGuessedCorrectly 的值被正确更新,这将按预期工作。

    checkIfGuessIsMyNumber(playerGuessedCorrectly);
    

    这就是你出错的地方。这会将当前存在的playerGuessedCorrectly 变量的值复制到checkIfGuessIsMyNumber 函数的参数中。在该函数中,playerGuessedCorrectly 的新副本被重新分配了不同的值,并且您有一个 return 语句,但请注意,您实际上从未使用过该函数的返回值。如果你改为这样做:

    playerGuessedCorrectly = checkIfGuessIsMyNumber(playerGuessedCorrectly);
    

    您的代码将按预期工作。您可能还想清理 checkIfGuessIsMyNumber 函数的主体:

    if(userGuess == computerNumber) {
      System.out.print("You have guessed my number.");
      return true;
    } else {
      return false;
    }
    

    【讨论】:

      【解决方案2】:

      Java 中的参数是按值传递。因此,当您调用playerGuessedCorrectly = false 时,您只需覆盖playerGuessedCorrectly 参数的本地副本。

      将逻辑封装在函数中的另一种方法是让它返回适当的值:

      private void print() {
          boolean playerGuessedCorrectly = false;
          while(!playerGuessedCorrectly) {
              playerGuessedCorrectly = checkIfGuessIsMyNumber();
          }
      }
      
      private boolean checkIfGuessIsMyNumber() {
         // userGuess is the next int entered by the user
         // computerNumber is a random number between 1 and 100
         if(userGuess == computerNumber) {
               System.out.print("You have guessed my number.");
               return true;
         } else {
               return false;
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 2021-01-12
        • 2020-07-18
        • 2017-03-10
        • 2011-08-01
        • 2013-04-22
        • 2017-11-20
        • 1970-01-01
        • 2015-05-24
        相关资源
        最近更新 更多