Can someone tell me what I did wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can someone tell me what I did wrong?

Look at my code, I'm practicing overriding. https://code.sololearn.com/c22E1nV3ZTVU/?ref=app

11th Feb 2018, 6:37 PM
Jaren Dogan
Jaren Dogan - avatar
3 Answers
+ 6
This method doesn't have a return statement for every possible route the code might take: static int min(int a, int b, int c, int d) { if(a > d) { return c; } else if ( d > c) { return b; } else if ( b > a) { return a; } else if(c > b) { return d; } } Because all the statements are if or else-if statements it is possible that none of them run and there isn't a return statement for that condition. Add a return statement to the end after the last else-if or change the last one to simply an else statement. You also have some logic problems if you're trying to actually return the minimum value from each of the methods in their current state.
11th Feb 2018, 6:47 PM
ChaoticDawg
ChaoticDawg - avatar
+ 17
solution for the logical error (for minimum number) if (a < b) { if (a < c) return a; else return c; } else { if (b < c) return b; else return c; } //thats for 3 variables , similarily U can form for 4 variables 👍
11th Feb 2018, 9:22 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
Thank you @ChaoticDawg, I forgot about that. Have a good day and everyone else who helps me
11th Feb 2018, 6:50 PM
Jaren Dogan
Jaren Dogan - avatar