Reputation: 3
I am very new to Java and am having some trouble. I feel like this an easy fix. Basically, I am looking to return a statement if two criteria are met. Below is my attached code.
boolean Windy = false;
if (Windy = true)
return "It is Windy";
else
return "Not Windy";
if (temperature < 30);
{return "Too Windy or Cold! Enjoy watching the weather through the window";
Additional errors are being thrown after I try to alter the script, claiming a return statement is required.
Upvotes: 0
Views: 102
Reputation: 34
You can compare criteria using "&&" = and "||" = or:
if (Windy && temperature < 30) {
return "it's windy and temperature is <30";
} else if (!Windy && temperature > 30) {
return "Not Windy";
}
Upvotes: 0
Reputation: 9336
Your code contains several mistakes:
=
in Windy = true
should be a ==
instead. =
is to assign something, and ==
is to check for equality.if (Windy = true) return "It is Windy"; else return "Not Windy";
will always return one of the two, the rest of your code below it is unreachable and will never be executed.if (temperature < 30);
should be removed.{}
-block inside your method isn't necessary.I think this is what you are looking for with your code:
boolean windy = false;
if(windy){
return "It is Windy";
} else if(temperature < 30){
return "Too Windy or Cold! Enjoy watching the weather through the window";
} else{
return "Not Windy";
}
Of course, setting windy
to false
hard-coded kinda makes the first check impossible to reach as well. But I assume it's just your example code and in your actual code you retrieve the windy
as class variable or method parameter.
In addition, since windy
itself is a boolean, the == true
is redundant and just if(windy)
would be enough.
PS: Variable names in Java are best practice to be camelCase, so use windy
instead of Windy
in this case.
I've also added the brackets around the if/else-if/else statements. They aren't required and you can leave them out if you really prefer, but it's easier to modify code without making errors later on.
Upvotes: 3
Reputation: 9437
OK, I checked your image (what I usually don't do) and spotted several issues in your code.
public static String getWeatherAdvice(int temperature, String description)
{
{ // REMOVE THIS BRACKET
if ( Windy = true) // = signifies an assigning a value, not a comparison. Use ==
return "It is Windy";
else
return "Not Windy";
if ( temperature < 30 ); // Since the above if-else will always return a value, this
// code can not be reached. Put this if before the previous if-else. Also: remove the ; after the if statement, otherwise, it ends the if, and the return statement might be triggered.
{ // don't put this bracket if you have a ; after the if
return "Too windy ... ";
} // don't put this bracket if you have a ; after the if
} // REMOVE THIS BRACKET
}
A corrected version of your code (could be):
public static String getWeatherAdvice(int temperature, String description)
{
if ( temperature < 30 )
{
return "Too windy ... ";
}
if ( Windy == true) // can also be written as: if ( Windy )
return "It is Windy";
else
return "Not Windy";
}
Upvotes: 0