Results 1 to 10 of 10
- 06-16-2011, 04:23 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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:
and so forth.Java 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();
How can I make this new method show the grade? Normally, I'd do something like this:
BUT I have to keep the scores as score1, score2, etc. because later on I calculate the average of the 5 scores they enter.Java 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';
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 #2
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 #3
Member
- Join Date
- Jun 2011
- Location
- San Diego, CA
- Posts
- 24
- Rep Power
- 0
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:
That is if you are sure there will be 5 scores. Else we'll need a slightly different loop.Java 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'; }
- 06-16-2011, 04:55 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Yes, only five.
OK so now I have this.
and I get these errors:Java 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; } }
LBGrade.java:65: cannot find symbol
symbol : variable score
location: class LBGrade
if (score[i] >= 90)
^
LBGrade.java:68: cannot find symbol
symbol : variable score
location: class LBGrade
else if (score[i] >=80)
^
LBGrade.java:71: cannot find symbol
symbol : variable score
location: class LBGrade
else if (score[i] >= 70)
^
LBGrade.java:74: cannot find symbol
symbol : variable score
location: class LBGrade
else if (score[i] >= 60)
^
LBGrade.java:81: incompatible types
found : char
required: java.lang.String
return result;
^
5 errors
- 06-16-2011, 05:20 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Can anyone help?
-
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 #7
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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 #8
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 #9
Also it looks to me as though you could simplify your code greatly by using loops.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 06-17-2011, 09:30 AM #10
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
Array with multiple variables - Problem with input
By Xinc in forum New To JavaReplies: 5Last Post: 03-17-2011, 04:19 PM -
Effect of Multiple Condtitions in if() statements to run time
By kimhonoridez in forum New To JavaReplies: 4Last Post: 12-08-2010, 08:40 AM -
Running multiple threads on multiple CPU cores?
By Dosta in forum Threads and SynchronizationReplies: 2Last Post: 09-19-2010, 03:48 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
Help with if else statements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks