I wrote a couple of if statements in Java. I wanted the first if statement to call a function called newGame()
and not enter another if statement if true.
This is my if statement:
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();
}
It looks like the first if statement is executed, and then the second one gets executed as well.
For some reason my if statements only work if I combine them even though the tests seem unrelated:
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();
}
This is the whole method:
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();
}
This is the separate newGame()
function that gets called:
public void newGame() {
numberOfTries = 8;
theNumber = (int) (Math.random() * 100 + 1);
}
I see the numberOfTries == 0
test as separate from the guess < theNumber
test. Why doesn't the first if test exclude the second if test?
There are two conditional statements in java- if
and if else
:
if (condition) {
<code to run>
}
if (condition) {
<code to run if condition is true>
} else {
<code to run if condition is false>
}
While you can make else if
statements, they are an abbreviated form of:
if (condition) {
<code to run if condition is true>
} else {
if (condition2) {
<code to run if condition is true (and previous condition is false)>
}
}
In none of those conditional statements does an if
statement rely on a previous if
statement unless it is inside of an else
statement.
While using else if
is customary (for every language because of its readablity), you could make each if statement dependant of the previous with:
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>
}
This is also slower than else if
statements, so I wouldn't recommend it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.