0

I am trying to write this program to get a user inputed number of grades up to 15. I then need to add the grades, find the largest, smallest and average of the grades, and finally display the results. Here is what I have so far. I can get past the input but when i get to processing thats where my code runs into problems.

import java.util.Scanner;

 public class steal_ExamList
 {
	    private final static int MAX_SCORE =15;
    	double number,small,large,average;
    	int count;
		private double list [];
    	
        public steal_ExamList()
        {
        	System.out.print("Welcome to the Exam Scores Program \n");
        	System.out.print ("\n");
        }
        
        void getScores()
        {       	
        	Scanner input=new Scanner(System.in);
        	System.out.print ( "How many scores will you enter (up to 15):");
            count = input.nextInt();
            System.out.print ("\n");
            
            while (count > MAX_SCORE)
            {
                    System.out.print("Error: You may onlyenter upto 15 scores - Please        try again:");
                    count=input.nextInt();
                    System.out.print ("\n");
            }
            
            list= new double [count];
            
            for(int i = 0;i< list.length; i++)
            {
            System.out.println("Enter score " + (i+1) + ": ");
            number = input.nextDouble();
            list[i] = number;
            }                              
        }
        
        void processScores(double number)
        {
        	 small = list [count];
        	
        	for (double number : list)
        	{
        		if (number < small)
        			small = number;
        	} 
        	
        	large = list [count];
        	
        	for (double number : list)
        	{
        		if (number > large)
        			large = number;
        	}
			
        	double total = 0;
        	
        	for(double number : list)
        		total += number;
        	
        	average = total/number;

        }
        
        void displayResults()
        {
        	System.out.println("\nAverage Score = " + average);
        }
 }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Akill10
0

You are declaring the same variable from your processScore() method params in all your enhanced loops. You cannot do that. The enhanced for loop needs to have a type and variable declared locally. You read the loop as:
(for each double in list)
//do something
It is not the same as a normal for loop where you would say, using this variable, loop until it equals this variable. You don't need to initialise it.

It's important to note that the enhanced for loop can't be used everywhere. You can't use the enhanced for loop:

* To remove elements as you traverse collections
* To modify the current slot in an array or list
* To iterate over multiple collections or arrays
http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.