簡體   English   中英

Java計算等級的程序

[英]program in java computing grades

我應該寫一個程序,根據要輸入的科目數量來計算學生的平均成績。

這是我的代碼,但不會在輸入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;
    }
  }
}

請幫我解決錯誤,謝謝

您的代碼ArrayIndexOutOfBoundException因為您正在初始化

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

在您獲得num2process之前,因此您已經初始化了double num2process=0.0因此它正在

double[] grade = new double[0.0];

因此,您需要按以下方式進行更改(采用num2process后初始化grade

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM