# if statements for multiple variables

• 06-16-2011, 04:23 AM
dookie1293
if statements for multiple variables
I need to make a new method to determine letter grades from data previously entered in the main method.

The way of entering was:

Code:

```System.out.println("Enter the first test score: ");         score1 = keyboard.nextInt();         System.out.println();         System.out.println("Enter the second test score: ");         score2 = keyboard.nextInt();         System.out.println();```
and so forth.

How can I make this new method show the grade? Normally, I'd do something like this:
Code:

```if (score >= 90)                 result = 'A';                                 else if (score >=80)                 result = 'B';                 else if (score >= 70)                 result = 'C';                 else if (score >= 60)                 result = 'D';                 else                 result = 'F';```
BUT I have to keep the scores as score1, score2, etc. because later on I calculate the average of the 5 scores they enter.

How do I make it calculate the grade, and how do I put it back in the main method so it displays the result from the calculateGrade method?
• 06-16-2011, 04:34 AM
Junky
You call the method from the main method and pass the value of the score. Your method then returns the corresponding letter grade to the main method where you store it in a variable.
• 06-16-2011, 04:37 AM
JDScoot
You could always store the scores in an array instead of individual variables. That way you can loop through the array and test each one against the if statement like so:
Code:

```for(int i = 0; i <= 5; i++){     if (score[i] >= 90)     result = 'A';                     else if (score[i] >=80)     result = 'B';     else if (score[i] >= 70)     result = 'C';     else if (score[i] >= 60)     result = 'D';     else     result = 'F'; }```
That is if you are sure there will be 5 scores. Else we'll need a slightly different loop.
• 06-16-2011, 04:55 AM
dookie1293
Quote:

Originally Posted by JDScoot

That is if you are sure there will be 5 scores. Else we'll need a slightly different loop.

Yes, only five.

OK so now I have this.

Code:

```import java.util.Scanner; public class LBGrade {         public static void main(String[] args)         {         int score1, score2, score3, score4, score5;         Scanner keyboard = new Scanner(System.in);         System.out.println("Enter 5 test scores, and I will tell you your grades and average.");         System.out.println();         System.out.println("Enter the first test score: ");         score1 = keyboard.nextInt();         System.out.println("You received the grade of " + determineGrade(score1, score2, score3, score4, score5) + " on this test.");         System.out.println();         System.out.println("Enter the second test score: ");         score2 = keyboard.nextInt();         System.out.println("You received the grade of " + determineGrade(score1, score2, score3, score4, score5) + " on this test.");         System.out.println();         System.out.println("Enter the third test score: ");         score3 = keyboard.nextInt();         System.out.println("You received the grade of " + determineGrade(score1, score2, score3, score4, score5) + " on this test.");         System.out.println();         System.out.println("Enter the fourth test score: ");         score4 = keyboard.nextInt();         System.out.println("You received the grade of " + determineGrade(score1, score2, score3, score4, score5) + " on this test.");         System.out.println();         System.out.println("Enter the fifth test score: ");         score5 = keyboard.nextInt();         System.out.println("You received the grade of " + determineGrade(score1, score2, score3, score4, score5) + " on this test.");         System.out.println();         System.out.println("The average of all five tests is " + calcAverage(score1, score2, score3, score4, score5) + ".");                         }         public static int calcAverage(int score1, int score2, int score3, int score4, int score5)         { int result;                 result = (score1 + score2 + score3 + score4 + score5) / 2;                 return result;         }         public static String determineGrade(int score1, int score2, int score3, int score4, int score5)                 {        char result;                                 for(int i = 0; i <= 5; i++)                 {                     if (score[i] >= 90)                     result = 'A';                                     else if (score[i] >=80)                     result = 'B';                     else if (score[i] >= 70)                     result = 'C';                     else if (score[i] >= 60)                     result = 'D';                     else                     result = 'F';                 }                 return result;         } }```
and I get these errors:
Quote:

symbol : variable score
if (score[i] >= 90)
^
symbol : variable score
else if (score[i] >=80)
^
symbol : variable score
else if (score[i] >= 70)
^
symbol : variable score
else if (score[i] >= 60)
^
found : char
required: java.lang.String
return result;
^
5 errors
• 06-16-2011, 05:20 AM
dookie1293
Can anyone help?
• 06-16-2011, 05:23 AM
Fubarable
The error tells you exactly what's wrong: you're using a variable that you've never declared, the score array. So if you're going to use a score array, you'd better first declare it.

Also, you bumped your question after only 15 or so minutes. You might want to show a little more patience. Please remember that we're all volunteers and no one likes to be pressured, least of all someone who is helping you for nothing.
• 06-16-2011, 05:25 AM
dookie1293
Thank you, and I know. I usually wouldn't do this (such as if I'm on the linux forums, I usually wait days for my answer) but I needed to get this done really fast. Thanks for your help.
• 06-16-2011, 06:05 AM
Junky
Quote:

Originally Posted by dookie1293
but I needed to get this done really fast.

Your time constraints are your problem. Do not impose them on us.

Why are you passing all 5 scores to the determineGrade method? That is not what I suggested in my first post. The last error is obvious. The method says it will return a String and yet you try to return a char.
• 06-16-2011, 06:09 AM
Dark
Also it looks to me as though you could simplify your code greatly by using loops.
• 06-17-2011, 09:30 AM