user2027323
user2027323

Reputation: 11

How can I include range in a conditional (if/then) statement?

I'm trying to write a program in Java that returns a letter grade when the grades for all quarters, as well as the grades for the midterms and finals are in. So far this is what it looks like:

public static void main (String args[])
{
   System.out.println("To figure out final grade answer these questions. Use only numbers, and include decimal points where applicable");
   Scanner g = new Scanner(System.in); 
   System.out.println("What was your quarter one grade?");
   int o = g.nextInt();
   System.out.println("What was your quarter two grade?");
   int t = g.nextInt(); 
   System.out.println("What was your quarter three grade?");
   int h = g.nextInt();
   System.out.println("What was your quarter four grade?");
   int r = g.nextInt();
   System.out.println("What was your grade on the midterm?");
   int m = g.nextInt();
   System.out.println("What was your grade on the final?");
   int f = g.nextInt();
   double c = 0.2 * o + 0.2 * t + 0.2 * h + 0.2 * r + 0.1 * m + 0.1 *f;
   if(c >= 95)
   {
        System.out.println("A+");
   } 
   else if(c = ?)
   {
       System.out.println("A");
   }  
}

}

I want to show a range of 90 to 94 in the last else if statement in the code. I was recommended I use Math.random as a command, but I don't know what equation to write so that it works within the range I mentioned. Any help would be much appreciated. Thanks in advance.

Upvotes: 1

Views: 18655

Answers (3)

Hamzeen Hameem
Hamzeen Hameem

Reputation: 2560

Here's a slightly different approach to generate the grades dynamically,

private static final String[] constants = {"F","D","C","B","A"};
public String getGrade(float score) {
    if(score < 0)
        throw new IllegalArgumentException(Float.toString(score));

    if((int)score <= 59)
        return constants[0];

    if((int)score >= 100)
        return constants[4];

    int res = (int) (score/10.0);
    return constants[res-5];
}

Upvotes: 0

M9A
M9A

Reputation: 3276

if(c >= 95)
   {
        System.out.println("A+");
   } 
   else if(c >= 90 && c <=94)
   {
       System.out.println("A");
   }  

EDIT you can get rid of && c <=94 if you want as you have already checked the upper bound

Upvotes: 0

nneonneo
nneonneo

Reputation: 179402

Since you are already testing c >= 95 in the first statement, you need only check the lower bound:

if(c >= 95) { /* A+ */ }
else if(c >= 90) { /* A */ }
else if(c >= 85) { /* A- */ }
...

Upvotes: 5

Related Questions