1

I'm having problems with the if then statemenT. I'm making a program that will tell the user the grade if he enters a integer, ex A if he gets 100. The problem with this program is that if I input 50, It will print both the F and D mark. I have a feeling its my bracket that are the culprit. If you can help, it would be great.

import java.util.Scanner;

public class LoanQualificationProgram{
   public static void main(String[] args){
      Scanner keyboard = new Scanner(System.in);
      int grade;
      grade = keyboard.nextInt();

      if (grade >=80) 
        System.out.println("Your grade is an A");
      else {
        if (grade >=70)
           System.out.println("Your grade is B");
        else {
          if (grade>=60)
             System.out.println("your grade is a C");
          else {
             if (grade >=50)
                System.out.println("your grade is a D");
             if (grade >=0)
                System.out.println("your grade is an F");
          }
        }
      }
1
  • 1
    You simply forgot the else after the condition checking for greater than 50. Commented Sep 28, 2015 at 2:14

2 Answers 2

4

You don't need to nest these statements. To use else-if effectively, try something like this:

if (grade >=80) {
  // A...
}
else if (grade >=70) {
  // B...
}
else if (grade>=60) {
  // C...
}
// ...
else {
  // Finally...
}
Sign up to request clarification or add additional context in comments.

Comments

2

try this bro or use case statement :)

if (grade >=80) 
  System.out.println("Your grade is an A");
else if(grade >=70)
    System.out.println("Your grade is B");
else if (grade>=60)
      System.out.println("your grade is a C");
else if (grade >=50)
        System.out.println("your grade is a D");
else if(grade >=0)
        System.out.println("your grade is an F");

2 Comments

Please fix the syntax error in your code. else (grade >=0) is not valid Java.
@ErwinBolwidt i add "if" on the last statement.. try it i think that cause the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.