Results 1 to 10 of 10
  1. #1
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default 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:

    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();
    and so forth.

    How can I make this new method show the grade? Normally, I'd do something like this:
    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';
    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?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    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.

  3. #3
    JDScoot is offline Member
    Join Date
    Jun 2011
    Location
    San Diego, CA
    Posts
    24
    Rep Power
    0

    Default

    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:
    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';
    }
    That is if you are sure there will be 5 scores. Else we'll need a slightly different loop.

  4. #4
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by JDScoot View Post

    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.

    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;
    
    	}
    
    }
    and I get these errors:
    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

  5. #5
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Can anyone help?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    dookie1293 is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by dookie1293 View Post
    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.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  10. #10
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    I can't help but notice that to calculate an "average" you're doing (score1 + score2 + score3 + score4 + score5) / 2.

    I suggest you re look at that; that wont give you an average.

Similar Threads

  1. Replies: 5
    Last Post: 03-17-2011, 04:19 PM
  2. Replies: 4
    Last Post: 12-08-2010, 08:40 AM
  3. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 03:48 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Help with if else statements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •