我的程序打印语句错误

问题描述 投票:0回答:3

当我运行程序并选择 0 到 100 之间的数字时,它会打印出错误的答案。

Java 控制台

 ----jGRASP 执行:java TestScores

你有多少次测试? 3
输入测试 1 的成绩:80
输入测试 2 的成绩:80
输入测试 3 的成绩:80
平均值为:26.666666666666668 平均值为:53.333333333333336 平均值为:80.0
 ----jGRASP:操作完成。
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");
    }
}
java loops printing
3个回答
3
投票

您在计算平均值的循环内打印平均值。

仅在循环外打印它。


1
投票

您应该在循环中计算总和,然后(在循环之后)将其除以元素数量。


0
投票

我将计算总和的语句从循环内部移到外部,这样就可以了。

我的新代码是。

导入java.util.Scanner;

公共课测试分数

{

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

    }

}

公共类 InvalidTestScore 扩展了 Exception { 公共无效测试分数() { super("错误:请输入 0 到 100 之间的数字"); } }

你可以关闭我的帖子。

© www.soinside.com 2019 - 2024. All rights reserved.