javaloopsprinting

My programs print statement is wrong


When i run my program and choose a number between 0 and 100, it prints my answer wrong.

Java console

 ----jGRASP exec: java TestScores

How many tests do you have? 3
Enter grade for Test 1: 80
Enter grade for Test 2: 80
Enter grade for Test 3: 80
The average is: 26.666666666666668The average is: 53.333333333333336The average is: 80.0
 ----jGRASP: operation complete.
import java.util.Scanner;

public class TestScores {

    public static void main(String[] args)
    {
        int numTests = 0;
        double[] grade = new double[numTests];
        double totGrades = 0;
        double average;
        int check = 1;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("How many tests do you have? ");
        numTests = keyboard.nextInt();
        grade = new double[(int) numTests];

        for (int index = 0; index < grade.length; index++)
        {
            System.out.print("Enter grade for Test " + (index + 1) + ": ");
            grade[index] = keyboard.nextDouble();

            if (grade[index] < 0 || grade[index] > 100)
            {
                try
                {
                    throw new InvalidTestScore();
                } 
                catch (InvalidTestScore e)
                {
                    e.printStackTrace();
                }
                break;
            }
        }

        for (int index = 0; index < grade.length; index++) {
            totGrades += grade[index];
            average = totGrades / grade.length;
            System.out.print("The average is: " + average);
        }
    }
}

public class InvalidTestScore extends Exception 
{
    public InvalidTestScore() 
    {
        super(" Error: Enter a number between 0 and 100");
    }
}

Solution

  • I move the statement which calculates the sum from inside the loop to the outside, that works.

    My new code is.

    import java.util.Scanner;

    public class TestScores

    {

        public static void main(String[]args)
    
        {
    
                int numTests = 0;
    
                double[] grade = new double[numTests];
    
                double totGrades = 0;
    
                double average;
    
                     int check = 1;
    
    
                Scanner keyboard = new Scanner(System.in);
    
    
                System.out.print("How many tests do you have? ");
    
                numTests = keyboard.nextInt();
    
    
    
                grade = new double[(int) numTests];
    
    
    
                for (int index = 0; index < grade.length; index++)
    
                {
    
                        System.out.print("Enter grade for Test " + (index + 1) + ": ");
    
                        grade[index] = keyboard.nextDouble();
    
    
                        if (grade[index] < 0 || grade[index]> 100)
    
                        {
                                        try 
    
                                          {
    
                                    throw new InvalidTestScore();
    
    
                                }
    
                                           catch (InvalidTestScore e) 
    
                                {
    
                                        e.printStackTrace();
    
                                }
                                            break;
    
                        }
    
                }
    
                    for (int index = 0; index < grade.length; index++)
                     {  
                            totGrades += grade[index];
    
    
    
                  }
    
                      average = totGrades/grade.length;
    
    
                      System.out.print("The average is: " + average);
    
        }
    

    }

    public class InvalidTestScore extends Exception { public InvalidTestScore() { super(" Error: Enter a number between 0 and 100"); } }

    You can close my post.