0

我应该编写一个程序,根据要输入的科目数量计算学生的平均成绩。

这是我的代码,但它不会在进入 subject1 的成绩部分执行:

import javax.swing.*;

public class Arrays {
  static  int ctr = 0;
  public static void main(String[] args) {
    String inputStr = "";
    double num2process = 0.0;
    double sum = 0.0, ave = 0.0;
    double[] grade = new double[(int) num2process];

    while (true) {
      try {
        inputStr = JOptionPane.showInputDialog(
            "Enter number of subjects to enter: ");
        num2process = Double.parseDouble(inputStr);

        while (ctr < num2process) {
          grade[ctr] = getNumber();
          sum += grade[ctr];
          ctr++; 
        }
      } catch (NumberFormatException err) {
        JOptionPane.showMessageDialog(
            null,
            "There is an error on entry",
            "Error Message", JOptionPane.WARNING_MESSAGE);
        continue;
      }
      break;    
    }

    // Accumulate the output.
    String output = "";
    int ctr2 = 0;

    output = "";

    while (ctr2 > num2process) {
      output += ("Grade on subject " + (ctr2+1)
                + " is " + grade[ctr]);
      ctr2++;
    }
    ave = sum / num2process;
    output += "\nAverage is " + ave;

    // Display the output.
    JOptionPane.showMessageDialog(
        null,
        output,
        "The result is",
        JOptionPane.INFORMATION_MESSAGE);
  }

  public static int getNumber() throws NumberFormatException {
    String inputStr = "";
    int num = 0;

    try {
      inputStr = JOptionPane.showInputDialog(
          "Enter grade on subject " + (ctr+1));
      num = Integer.parseInt(inputStr);
      return num;
    } catch (NumberFormatException errorAgain) {
      throw errorAgain;
    }
  }
}

请帮我解决错误谢谢

4

1 回答 1

0

您的代码正在抛出ArrayIndexOutOfBoundException,因为您正在初始化

double[] grade = new double[(int) num2process];

在你得到之前num2process,所以你已经初始化double num2process=0.0 了,所以它正在

double[] grade = new double[0.0];

因此,您需要将其更改如下(grade服用后初始化num2process

            String inputStr = "";
            double num2process = 0.0;
            double sum = 0.0, ave = 0.0;

            double[] grade ;          //Just Make an Instance of it

            while(true)
            {
                try
                {
                    inputStr = JOptionPane.showInputDialog("Enter number of subjects to enter: ");
                    num2process = Double.parseDouble(inputStr);
                    grade = new double[(int) num2process];     //Initialize it Here
                    while(ctr<num2process)
                    {
                        grade[ctr]= getNumber();
                        sum += grade[ctr];
                        ctr++; 
                    }

                }
                catch(Exception e){
                    //Your Exception Handling
                }
            }

编辑:正如您评论的:

它应该显示科目 1 到科目 3 的成绩。平均值很好,但我怎样才能将它减少到小数点后两位

这是为您准备的整个程序:

import java.text.DecimalFormat;

import javax.swing.*;

public class ArrayWithException 
{
        static  int ctr = 0;
        public static void main(String[] args)
        {
            String inputStr = "";
            double num2process = 0.0;
            double sum = 0.0, ave = 0.0;
            double[] grade ;

            while(true)
            {
                try
                {
                    inputStr = JOptionPane.showInputDialog("Enter number of subjects to enter: ");
                    num2process = Double.parseDouble(inputStr);
                    grade = new double[(int) num2process];
                    marks = new double[(int) num2process];
                    while(ctr<num2process)
                    {
                        grade[ctr] = getNumber();
                        sum += grade[ctr];
                        ctr++; 
                    }

                }
                catch (NumberFormatException err)
                {
                    JOptionPane.showMessageDialog(null, "There is an error on entry",
                        "Error Message", JOptionPane.WARNING_MESSAGE);
                    continue;
                }
                break;      

            }

            //accumulate the output
            String output="";
            int ctr2 = 0;

            output = "";

            while(ctr2 > num2process)
            {
                output +=("Grade on subject " +(ctr2+1) + " is "
                                + grade[ctr]);
                ctr2++;

            }

            ave = sum / num2process;
            DecimalFormat df=new DecimalFormat("##.##");   //Here it defines the pattern to follow
            ave=Double.parseDouble(df.format(ave));        //ave is formatted like df
            output +="\nAverage is " + ave;
            for(int i=0;i<grade.length;i++){ 
                output+="\nGrade for Subject "+(i+1)+" is: "+grade[i];
            }

            // display the output

            JOptionPane.showMessageDialog(null,output, "The result is",
                        JOptionPane.INFORMATION_MESSAGE);

        }

        public static int getNumber() throws NumberFormatException
        {
            String inputStr="";
            int num = 0;

            try
            {
                inputStr = JOptionPane.showInputDialog("Enter grade on subject " + (ctr+1));
                num = Integer.parseInt(inputStr);

                return num;

            }
            catch (NumberFormatException errorAgain)
            {
                throw errorAgain;
            }
        }
}
于 2012-12-09T05:51:21.817 回答