2012-12-09 60 views
0

我應該編寫一個程序,根據要輸入的主題數計算學生的平均成績。java計算等級中的程序

這裏是我的代碼,但它不進入年級的部分執行上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; 
    } 
    } 
} 

請幫我解決這個錯誤感謝

+1

關於'「但它並沒有在subject1進入年級的部分執行:」' - 你可以詳細一點?你的問題到底是什麼? –

+3

爲什麼'getNumber()'拋出*和*捕獲'NumberFormatException'? –

+0

此外,主方法內catch塊中的「continue」關鍵字不會執行任何操作。我認爲你需要從基礎開始。 –

回答

0

你的代碼拋出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; 
      } 
     } 
} 
+1

@Doha Kik:如果你覺得它有用,你可以將它和[標記爲已接受](http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work)...: ) – Parth

+0

它終於工作。兄弟你能幫助我輸出。它應該顯示主題1的等級,以主題3.平均很好,但我怎麼能減少它兩位小數只感謝請 –

+0

@DohaKik:檢查出來..我已經編輯答案,upvote它和[標記爲接受](http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work)... :) – Parth