Java Programming Boolean if-else statement

In summary, The code is checking for the values of two boolean variables, isRed and isBalloon, and prints different statements based on their values. If isBalloon is true and isRed is false, it prints "Balloon". If both variables are true, it prints "Red balloon". If both variables are false, it prints "Not a balloon". The speaker suggests checking for the true values first to simplify the code.
  • #1
dellmac
4
0
Looking for some assistance with this. I've been messing with this for hours and can't seem to get it right. Some assistance would be greatly appreciated. What I have below is not correct. Material in green cannot be changed. Thanks!Print "Balloon" if isBalloon is true and isRed is false. Print "Red balloon" if isBalloon and isRed are both true. Print "Not a balloon" otherwise. End with newline.import java.util.Scanner;

public class RedBalloon {
public static void main (String [] args) {
boolean isRed = false;
boolean isBalloon = false;


if (!isBalloon || isRed) {
System.out.println("Balloon");
}

else if (!isBalloon && !isRed) {
System.out.println("Red balloon");
}

else {
System.out.println("Not a balloon");
}

return;
}
}
 
Technology news on Phys.org
  • #2
Hi,

The logical operator "!" is a negation in Java, so you are doing just the opposite you are asked for.

The boolean "!isBalloon" is true if "isBalloon" is false, and vice versa.

My recommendation, check first whether the two booleans are true and print "Red ballon", in this way you will only need to check one boolean in the next if condition, I think that way is easier (Just eliminating one logical operator).
 
  • #3
Alright thank you! Your response gave me a great understanding, and I have it now. Really appreciate the assistance!
 

Related to Java Programming Boolean if-else statement

1. What is a Boolean if-else statement in Java programming?

A Boolean if-else statement is a control structure in Java that allows a program to make decisions based on a condition. It consists of an if statement, which executes a block of code if the condition is true, and an optional else statement, which executes a different block of code if the condition is false.

2. How do you write a Boolean if-else statement in Java?

To write a Boolean if-else statement in Java, you first need to define the condition inside the if statement. For example, you could use a comparison operator like "==" to check if two values are equal. Then, you can specify the code to be executed if the condition is true inside the curly braces after the if statement. If you want to execute a different block of code if the condition is false, you can add an else statement and specify the code within its curly braces.

3. What is the purpose of using a Boolean if-else statement?

The purpose of a Boolean if-else statement is to control the flow of a program based on a condition. By using this statement, a program can execute different blocks of code depending on whether the condition is true or false. This allows for more complex and dynamic programming logic.

4. Can a Boolean if-else statement be nested?

Yes, a Boolean if-else statement can be nested, meaning it can be placed inside another if statement. This allows for more complex conditions and decision-making in a program. However, it is important to keep the code organized and avoid excessive nesting to ensure readability and maintainability.

5. What happens if the condition in a Boolean if-else statement is not met?

If the condition in a Boolean if-else statement is not met, meaning it evaluates to false, then the code inside the else statement (if present) will be executed. If there is no else statement, then the program will continue to the next line of code after the if-else statement. If the condition is true, then the code inside the if statement will be executed and the code inside the else statement (if present) will be skipped.

Similar threads

Back
Top