我的程序打印语句是错误的 [英] My programs print statement is wrong

查看:132
本文介绍了我的程序打印语句是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Java控制台


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


推荐答案

I

我的新代码是

import java.util.Scanner;

import java.util.Scanner;

public class TestScores

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

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

您可以关闭我的讯息。

这篇关于我的程序打印语句是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆