Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Methods Help - Unreachable statement error

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Methods Help - Unreachable statement error

    The assignment is this:

    Write a program that asks the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program:

    calcAverage-This method should accept 5 test scores as arguments and return the average of the scores.

    determineGrade-This method should accept a test score as an argument and return a letter grade for the score.


    import java.util.*;
     
     
     
    public class GradeProgram
     
    		{
     
    			public static void main(String[]args)
     
    				{
    					// Declare variables
    					double average=0;
    					int score1=0;
    					int score2=0;
    					int score3=0;
    					int score4=0;
    					int score5=0;
    					char grade1;
    					char grade2;
    					char grade3;
    					char grade4;
    					char grade5;
     
     
    					// Get the scores from the user
    					Scanner keyboard = new Scanner(System.in);
     
    					System.out.println("Enter the first score");
    					score1 = keyboard.nextInt();
     
    					System.out.println("Enter the second score");
    					score2 = keyboard.nextInt();
     
    					System.out.println("Enter the third score");
    					score3 = keyboard.nextInt();
     
    					System.out.println("Enter the fourth score");
    					score4 = keyboard.nextInt();
     
    					System.out.println("Enter the fifth score");
    					score5 = keyboard.nextInt();
     
     
     
    					// Call calcAverage method
    					average = calcAverage(score1, score2, score3, score4, score5);
     
    					//Display the average
    					System.out.println("The average is " + average);
     
    					// Call determineGrade method
    					grade1 = determineGrade(score1, score2, score3, score4, score5);
    					grade2 = determineGrade(score1, score2, score3, score4, score5);
    					grade3 = determineGrade(score1, score2, score3, score4, score5);
    					grade4 = determineGrade(score1, score2, score3, score4, score5);
    					grade5 = determineGrade(score1, score2, score3, score4, score5);
     
     
     
     
     
    					// Display the letter grades
    					System.out.println("Score " + "Grade");
    					System.out.println(score1 + " " + grade1);
    					System.out.println(score2 + " " + grade2);
    					System.out.println(score3 + " " + grade3);
    					System.out.println(score4 + " " + grade4);
    					System.out.println(score5 + " " + grade5);
     
     
     
     
    				}
     
     
    			// Calculates average of test scores
    			public static double calcAverage (int score1, int score2, int score3, 
    											 	 		 int score4, int score5)
     
    				{ 
     
    					// Calculate average
    					int total=0;
    					double average=0;
     
    					total = score1 + score2 + score3 + score4 + score5;
    					average = total / 5;
     
    					// Return average to main program
    					return average;
     
     
    				}
     
     
    			// Returns letter grade for test score
    			public static char determineGrade (int score1, int score2, int score3,
    														  int score4, int score5)
     
    				{
    					// Declare variables to hold grades in
    					char grade1;
    					char grade2;
    					char grade3;
    					char grade4;
    					char grade5;
     
    					// Determines grade of first score
    					if (score1>=90 && score1<=100)
    							{ grade1 = 'A'; }
    					else if (score1>=80 && score1<90)
    							{ grade1 = 'B'; }
    					else if (score1>=70 && score1<80)
    							{ grade1 = 'C'; }
    					else if (score1>=60 && score1<70)
    							{ grade1 = 'D'; }
    					else 
    							{ grade1 = 'F'; }
     
     
    					// Determines grade of second score
    					if (score2>=90 && score2<=100)
    							{ grade2 = 'A'; }
    					else if (score2>=80 && score2<90)
    							{ grade2 = 'B'; }
    					else if (score2>=70 && score2<80)
    							{ grade2 = 'C'; }
    					else if (score2>=60 && score2<70)
    							{ grade2 = 'D'; }
    					else
    							{ grade2 = 'F'; }
     
     
    					// Determines grade of third score
    					if (score3>=90 && score3<=100)
    							{ grade3 = 'A'; }
    					else if (score3>=80 && score3<90)
    							{ grade3 = 'B'; }
    					else if (score3>=70 && score3<80)
    							{ grade3 = 'C'; }
    					else if (score3>=60 && score3<70)
    							{ grade3 = 'D'; }
    					else
    							{ grade3 = 'F'; }
     
     
    					// Determines grade of fourth score
    					if (score4>=90 && score4<=100)
    							{ grade4 = 'A'; }
    					else if (score4>=80 && score4<90)
    							{ grade4 = 'B'; }
    					else if (score4>=70 && score4<80)
    							{ grade4 = 'C'; }
    					else if (score4>=60 && score4<70)
    							{ grade4 = 'D'; }
    					else
    							{ grade4 = 'F'; }
     
     
    					// Determines grade of fifth score
    					if (score5>=90 && score5<=100)
    							{ grade5 = 'A'; }
    					else if (score5>=80 && score5<90)
    							{ grade5 = 'B'; }
    					else if (score5>=70 && score5<80)
    							{ grade5 = 'C'; }
    					else if (score5>=60 && score5<70)
    							{ grade5 = 'D'; }
    					else
    							{ grade5 = 'F'; }
     
     
    					// Returns grades of tests
    					return grade1;
    					return grade2;
    					return grade3;
    					return grade4;
    					return grade5;
     
     
     
    				}
     
     
    		}



    I get an "unreachable statement" error for return grade2, grade3, grade4, grade5. The program completely compiles and runs for the first grade to be displayed but no more. Any help is appreciated! Thanks.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Methods Help - Unreachable statement error

    A method can only return one thing. Once the first return statement is executed it returns to the calling method. How is supposed to go back and execute all those other return statements?

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Methods Help - Unreachable statement error

    a function can return only one value
    you are returning 5 integers that is why it is giving error

Similar Threads

  1. Area of a triangle (using 2 extra methods) error help
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2010, 07:08 PM
  2. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 06:35 AM
  3. another unreachable statement..
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2009, 01:15 PM
  4. unreachable statement Again?
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 1st, 2009, 11:23 AM
  5. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 11:06 AM