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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top