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; } }
6 Answers
+ 7
if (bool == true)
Change that and it might work
+ 6
The returning of y is already with else statement prepared.
+ 5
Your condition:
if (Boolean.True)
Is always true, so the "else" branch is never activated. You are not using the "bool" variable anywhere.
+ 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);
+ 2
It worked,thank you!
0
How can I implement return y?