0

Help with Java

Why only half of my code is working? public int ifExample(boolean bool,int x, int y){ if (Boolean.True){ return x; } else{ return y; } }

14th May 2023, 2:19 PM
Olga Restea
6 Réponses
+ 7
if (bool == true) Change that and it might work
14th May 2023, 2:35 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 6
The returning of y is already with else statement prepared.
14th May 2023, 3:05 PM
JaScript
JaScript - avatar
+ 5
Your condition: if (Boolean.True) Is always true, so the "else" branch is never activated. You are not using the "bool" variable anywhere.
14th May 2023, 2:21 PM
Tibor Santa
Tibor Santa - avatar
+ 3
A few optional side notes: * if(bool == true) can just be written as if(bool) * single-statement ifs can be written without the {} Advanced mode for later: the "ternary operator", ?, lets you condense very simple if/elses even further: return(bool ? x : y);
14th May 2023, 3:13 PM
Orin Cook
Orin Cook - avatar
+ 2
It worked,thank you!
14th May 2023, 3:05 PM
Olga Restea
0
How can I implement return y?
14th May 2023, 2:30 PM
Olga Restea