my java method is requiring a return statement after the if else statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

my java method is requiring a return statement after the if else statement

static int checkScore(int score){ if (score >= 70 && score <= 100){ return 4; System.out.println("A"); } else if (score >=60 && score <=69){ return 3; System.out.println("B"); } else if (score >=50 && score <=59){ return 2; System.out.println("C"); } else if (score >=40 && score <=49){ return 1; System.out.println("D"); } else if (score >=0 && score <=44){ return 0; System.out.println("E"); } else{System.out.println("Enter correct score"); } }

3rd Aug 2021, 8:34 PM
Adewale IyanuOluwa
Adewale IyanuOluwa - avatar
4 Answers
+ 2
Adewale IyanuOluwa If you have return type int then you have to return something. Your first SOP statement is showing error because you are printing after returning value. So SOP will never execute because program returned before that. No need to return in every if else. Just declare a variable, assign value then return at the end of if else statement. If you don't want to return then don't call your method in this case. So finally, your code should be like this: https://code.sololearn.com/cV2swDa7D9Ig/?ref=app
3rd Aug 2021, 10:40 PM
A͢J
A͢J - avatar
+ 1
Yes you have to return an integer value like -1 in final else. And first use System.out then return
3rd Aug 2021, 9:02 PM
▬▬▬
▬▬▬ - avatar
+ 1
Thank you so much. This really help a lot.A͢J - S͟o͟l͟o͟ H͟e͟l͟p͟e͟r͟
4th Aug 2021, 4:48 PM
Adewale IyanuOluwa
Adewale IyanuOluwa - avatar
0
NotHuman i don't want it to return anything if the score range is less than 0 and greater than 100. I returned -1, but my first S.O.P statement is showing an error message of "Unreachable Statement"
3rd Aug 2021, 9:18 PM
Adewale IyanuOluwa
Adewale IyanuOluwa - avatar