有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么if语句不排除Java中的另一个if语句?

我用Java编写了几个if语句。我希望第一个if语句调用名为newGame()的函数,如果为true,则不要输入另一个if语句

这是我的if声明:

   if (numberOfTries == 0) {
       message = "You lost! Try again!";
       System.out.println("Setting label you lost: " + message);
       newGame();
   }
   if (guess < theNumber) {
       message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
       System.out.println("Setting label too low: " + message);
   }
   else if (guess > theNumber) {
       message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
       System.out.println("Setting label too high: " + message);
   }
   else {
       message = guess + " is correct. Let's play again!";
       System.out.println("Setting label is correct: " + message);
       newGame();
   }

它看起来像是第一个if语句被执行,然后第二个if语句也被执行

出于某种原因,我的if语句只有在组合它们时才起作用,即使测试似乎不相关:

   if (numberOfTries == 0) {
       message = "You lost! Try again!";
       System.out.println("Setting label you lost: " + message);
       newGame();
   }
   else if (guess < theNumber) {
       message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
       System.out.println("Setting label too low: " + message);
   }
   else if (guess > theNumber) {
       message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
       System.out.println("Setting label too high: " + message);
   }
   else {
       message = guess + " is correct. Let's play again!";
       System.out.println("Setting label is correct: " + message);
       newGame();
   }

这就是整个方法:

public void checkGuess() {
    String guessText = txtGuess.getText();
    txtGuess.setText("");// Empties the contents of the text field.
    String message = "";
    try {
        int guess = Integer.parseInt(guessText);
        if (numberOfTries == 0) {
            message = "You Lost! A new game has begun and you have 8 guesses remaining.";
            newGame();
        }
        else if (guess < theNumber) {
            message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
        }
        else if (guess > theNumber) {
            message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
        }
        else {
            message = guess + " is correct. Let's play again!";
            newGame();
        }
    } catch (Exception e) {
        message = "Enter a whole number between 1 and 100.";
    } finally {
        lblOutput.setText(message);
        txtGuess.requestFocus();
        txtGuess.selectAll();
        
    }
    decrementNumberOfTries();
}

这是被调用的单独的newGame()函数:

public void newGame() {
    numberOfTries = 8;
    theNumber = (int) (Math.random() * 100 + 1);
}

我认为numberOfTries == 0测试与guess < theNumber测试是分开的。为什么第一个if测试不排除第二个if测试


共 (1) 个答案

  1. # 1 楼答案

    java中有两个条件语句ifif else

    if (condition) {
        <code to run>
    }
    
    
    if (condition) {
        <code to run if condition is true>
    } else {
        <code to run if condition is false>
    }
    

    虽然您可以使用else if语句,但它们是以下语句的缩写形式:

    if (condition) {
        <code to run if condition is true>
    } else {
        if (condition2) {
            <code to run if condition is true (and previous condition is false)>
        }
    }
    

    在这些条件语句中,if语句都不依赖于前面的if语句,除非它在else语句中

    虽然使用else if是一种习惯(因为每种语言的可读性,所以它适用于每种语言),但您可以使每个if语句依赖于前一个if语句:

    if (condition1) {
        <code to run if true>
    }
    if (condition2 && !condition1) {
        <code to run if condition1 is false and condition2 is true>
    }
    if (condition3 && !condition2 && !condition1) {
        <code to run if condition1 and condition2 are false and condition3 is true>
    }
    

    这也比else if语句慢,所以我不推荐它