Assign the value to the variable in Java

if(rain && temperature < 0)
    snow = true;

But you also need to set snow = false by default, so:

public class Q8 {

boolean snowForecast() {
    boolean snow = false; 
    int temperature=-5;
    boolean rain=true;
    if(rain && temperature < 0)
        snow = true;
    return snow;
}
}

Right now, this is what your doing:

1) Initializing variables.

2) ONLY returning a VALUE if it will snow

Step two is where you go wrong. You should return false if there is not snow and true if there is snow. I think you’re misunderstanding something very basic. I’m not sure what it is, but maybe you should look into a Java book or tutorial series.

Functions return a value. Boolean functions return true or false, with return true and return false respectively. You need to be returning SOMETHING, not just an empty variable (You actually can’t return an empty variable in Java, but that’s what you’re trying to do).

An alternative way of writing it that might be easier for you to understand is this:

public class Q8 {

boolean snowForecast() {
    boolean snow = false; 
    int temperature=-5;
    boolean rain=true;
    if(rain && temperature < 0) {
        return true;
    } else {
        return false;
    } 
}
}

IMPORTANT You really should learn at least the very basics of a language before posting on Stack Overflow. It makes it seem like you’re not putting in any effort. As in, you just want us to write your code for you. Maybe add in what error message you’re getting and why you’re confused.

Leave a Comment