r/javahelp • u/emokii • Jul 03 '19
Solved Why is my If, else if statement skipping the if part?
I have an if, else if statement written in my code
If(exam.getGrade()>70) { System.out.println('P'); } else if(exam.getGrade()<70) { System.out.println('F'); }
I tried changing the code around. The numbers. Nothing worked. I can't figure out why it prints out 'F' when in the full code, the exam score is 90
0
Upvotes
1
u/crypt0herb Jul 03 '19
Typically else if is followed by else. Try replace elseif with else and see what happens. You can alternatively achieve the same using switch-case on exam.getGrade(). This is a pretty straight forward example of conditional logic.
3
u/_DTR_ Jul 03 '19
What does the implementation of your
getGrade()method look like? At a glance nothing seems out of place, though if the score is exactly 70 you'll skip both conditions. One of them should be>=/<=