My programs print statement is wrong

旧街凉风 提交于 2020-01-05 03:32:08

问题


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");
    }
}

回答1:


You print the average inside the loop that calculates the average.

Print it only outside the loop.




回答2:


You should calculate sum in loop and then (after the loop) divide it by the number of elements.




回答3:


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.



来源:https://stackoverflow.com/questions/5081240/my-programs-print-statement-is-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!