Java Java Programming Boolean if-else statement

  • Thread starter Thread starter dellmac
  • Start date Start date
  • Tags Tags
    Java Programming
AI Thread 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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top