Jump to content

Plese help JAVA Bolean not working

Symbolls
Go to solution Solved by Eigenvektor,

That code seems pretty complicated for what you're asked to do. How about using AND (&&) and OR (||), rather than XOR?

 

final boolean isCold = input.nextBoolean();
final boolean isDry = input.nextBoolean();

// For less mind-fuckery
final boolean isHot = !isCold;

if (isHot) {
    System.out.print("It is hot\n");
}
if (isDry) {
    System.out.print("It is dry\n");
}
if (isHot || isDry) {
    System.out.print("It is hot or dry\n");
}
if (isHot && isDry) {
    System.out.print("It is hot and dry\n");
}

 

I dont know what to do i need to make this exercise for univarcity and the teacher did not tell us how to do them and i need help plese

512518253_Screenshot2022-10-14212118.png.3c9775151899a33e2d03bad8d8abe287.png

import java.util.Scanner;

public class Exercise {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        boolean isCold = input.nextBoolean(); //Is it cold? [true/false]
        boolean isDry = input.nextBoolean(); //Is it dry? [true/false]
        boolean isHard = isDry ^ isCold;
        if (isCold == isDry){
            System.out.print("It is dry\n" +
                    "It is hot or dry");
        }else{

            if (!isCold) {
                System.out.print("It is hot\n" +
                        "It is dry\n" +
                        "It is hot or dry\n" +
                        "It is hot and dry");
                }
            if (!isHard){
                System.out.print("It is hot\n" +
                    "It is hot or dry");
            }
            }

        /* if (isDry^isCold){
            System.out.print("It is hot\n" +
                    "It is dry\n" +
                    "It is hot or dry\n" +
                    "It is hot and dry");
        }*/
    }
}

771802408_Screenshot2022-10-14212152.png.e25511d1406eb0863a4f9c8eec3f8747.png

Link to comment
Share on other sites

Link to post
Share on other sites

I would recommend you look up what the correct operators are first as a very basic step. https://www.w3schools.com/java/java_operators.asp

 

Then you should think about all the possibilities, the program can read 2 booleans isCold and isDry. That means there are 4 possible outcomes.

 

isCold = true AND isDry = false

isCold = true AND isDry = true

isCold = false AND isDry = false

isCold = false AND isDry = true

 

Now you can already see your if statements right there, because that is all the program can read. You just have to figure out what the logical outcomes are of each case. 

 

Here's the first one:

 

if (isCold && !isDry) {

  // no output

}

 

hope you can take it from here 😉

 

Link to comment
Share on other sites

Link to post
Share on other sites

That code seems pretty complicated for what you're asked to do. How about using AND (&&) and OR (||), rather than XOR?

 

final boolean isCold = input.nextBoolean();
final boolean isDry = input.nextBoolean();

// For less mind-fuckery
final boolean isHot = !isCold;

if (isHot) {
    System.out.print("It is hot\n");
}
if (isDry) {
    System.out.print("It is dry\n");
}
if (isHot || isDry) {
    System.out.print("It is hot or dry\n");
}
if (isHot && isDry) {
    System.out.print("It is hot and dry\n");
}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, merco said:

that does make it very neat 

Yeah. If you find you have to think about what a variable means every time you want to use it (hot… so… not cold) then it's a good pointer that you should rename it to something more appropriate.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×