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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top