Java Java Programming Boolean if-else statement

  • Thread starter Thread starter dellmac
  • Start date Start date
  • Tags Tags
    Java Programming
Click For Summary
The discussion revolves around troubleshooting a Java program that is intended to print specific messages based on the boolean values of `isBalloon` and `isRed`. The user is struggling with the logic in their conditional statements. Key points include the clarification that the negation operator "!" in Java inverses the boolean value, leading to incorrect outputs. The suggestion provided emphasizes checking the conditions for both booleans first to simplify the logic. Ultimately, the user expresses gratitude for the guidance, indicating that the explanation helped them resolve their issue.
dellmac
Messages
4
Reaction score
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
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).
 
Alright thank you! Your response gave me a great understanding, and I have it now. Really appreciate the assistance!
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 7 ·
Replies
7
Views
11K
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
2
Views
51K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
13K