2014-03-19 80 views
0

我的代碼幾乎是完美的......我想。我需要輸入用戶輸入的測試分數(最多50分),然後當用戶輸入-1時,它會顯示他們輸入的所有分數,從最低到最高排序,然後顯示在列表底部說「平均爲_ _」。不幸的是,當我運行它時,我得到了一個很大的0列表,我無法弄清楚我出錯的地方。如果任何人有任何想法,我將不勝感激平均成績:獲得0代替用戶輸入

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import java.text.DecimalFormat; 

public class AverageGrades extends JFrame 
{ 
//construct conponents 
static JLabel title = new JLabel("Average of Grades"); 
static JTextPane textPane = new JTextPane(); 
static int numberOfGrades = 0; 
static int total = 0; 
static DecimalFormat twoDigits = new DecimalFormat ("##0.00"); 

//set array 
static int[] grades = new int[50]; 

//create content pane 
public Container createContentPane() 
{ 
    //create JTextPane and center panel 
    JPanel northPanel = new JPanel(); 
    northPanel.setLayout(new FlowLayout()); 
    northPanel.add(title); 

    JPanel centerPanel = new JPanel(); 
    textPane = addTextToPane(); 
    JScrollPane scrollPane = new JScrollPane(textPane); 
     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     scrollPane.setPreferredSize(new Dimension(500,200)); 
    centerPanel.add(scrollPane); 

    //create Container and set attributes 
    Container c = getContentPane(); 
     c.setLayout(new BorderLayout(10,10)); 
     c.add(northPanel,BorderLayout.NORTH); 
     c.add(centerPanel,BorderLayout.CENTER); 

    return c; 
} 

//method to add new text to JTextPane 
public static JTextPane addTextToPane() 
{ 
    Document doc = textPane.getDocument(); 
    try 
    { 
     // clear previous text 
     doc.remove(0,doc.getLength()); 

     //insert title 
     doc.insertString(0,"Grades\n",textPane.getStyle("large")); 

     //insert grades and calculate average 
     for(int j=0; j<grades.length; j++) 
     { 
      doc.insertString(doc.getLength(), grades[j] + "\n", textPane.getStyle("large")); 
     } 
    } 
    catch(BadLocationException ble) 
    { 
     System.err.println("Couldn't insert text"); 
    } 

    return textPane; 
} 

//method to sort array 
public void grades(int grdArray[]) 
{ 
    //sort int array 
    for (int pass = 1; pass<grdArray.length; pass++) 
    { 
     for (int element = 0; element<grdArray.length -1; element++) 
     { 
      swap(grades, element, element + 1); 

     } 
    } 
     addTextToPane(); 

} 


//method to swap elements of array 
public void swap(int swapArray[], int first, int second) 
{ 
    int hold; 
    hold = swapArray[first]; 
    swapArray[first] = swapArray[second]; 
    swapArray[second] = hold; 
} 

//execute method at run time 
public static void main(String args[]) 
{ 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    AverageGrades f = new AverageGrades(); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    //accept first grade 
    int integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a grade (0-100) or -1 to calculate the average")); 

    //while loop accepts more grades, keeps count, and calulates the total 
    int count = 0; 
    int[] grades = new int[50]; 
    int num = 0; 
    while (count<50 && num!= -1) 
    { 
     num = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a grade (0-100) or -1 to calculate the average" + (count+1))); 
     if(num!=-1) 
      grades[count] = num; 
     count++; 

    } 

    //create content pane 
    f.setContentPane(f.createContentPane()); 
    f.setSize(600,375); 
    f.setVisible(true); 


} 

}

回答

0

這裏是更正瞭解決您的問題的代碼。

有兩個主要問題,您使用的是本地數組等級,而不是您的靜態全局數組等級。 (所以從主要刪除聲明)

其次,你從來沒有插入第一個值integerInput導致第一個元素丟失。

 import java.awt.*; 
    import javax.swing.*; 
    import javax.swing.text.*; 
    import java.text.DecimalFormat; 

    class AverageGrades extends JFrame { 
     // construct conponents 
     static JLabel title = new JLabel("Average of Grades"); 
     static JTextPane textPane = new JTextPane(); 
     static int numberOfGrades = 0; 
     static int total = 0; 
     static DecimalFormat twoDigits = new DecimalFormat("##0.00"); 

     // set array 
     static int[] grades = new int[50]; 

     // create content pane 
     public Container createContentPane() { 
      // create JTextPane and center panel 
      JPanel northPanel = new JPanel(); 
      northPanel.setLayout(new FlowLayout()); 
      northPanel.add(title); 

      JPanel centerPanel = new JPanel(); 
      textPane = addTextToPane(); 
      JScrollPane scrollPane = new JScrollPane(textPane); 
      scrollPane 
        .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
      scrollPane.setPreferredSize(new Dimension(500, 200)); 
      centerPanel.add(scrollPane); 

      // create Container and set attributes 
      Container c = getContentPane(); 
      c.setLayout(new BorderLayout(10, 10)); 
      c.add(northPanel, BorderLayout.NORTH); 
      c.add(centerPanel, BorderLayout.CENTER); 

      return c; 
     } 

     // method to add new text to JTextPane 
     public static JTextPane addTextToPane() { 
      int counter = 0; 
      Document doc = textPane.getDocument(); 
      try { 
       // clear previous text 
       doc.remove(0, doc.getLength()); 

       // insert title 
       doc.insertString(0, "Grades\n", textPane.getStyle("large")); 

//BREAKING IF NUMBER IS 0, SIGNIFIES THAT ALL ELEMENTS ARE COVERED 
//counter IS USED TO CALCULATE THE TOTAL NUMBER OF ELEMENTS 
       // insert grades and calculate average 
       for (int j = 0; j < grades.length; j++) { 
        if (grades[j] != 0) { 
         doc.insertString(doc.getLength(), grades[j] + "\n", 
           textPane.getStyle("large")); 
         counter++; 
        } else { 
         break; 
        } 
       } 

//THIS LINE INSERTS THE AVERAGE IN THE PANEL 
       doc.insertString(doc.getLength(), "Average is :" 
         + (total/counter) + "\n", textPane.getStyle("large")); 

      } catch (BadLocationException ble) { 
       System.err.println("Couldn't insert text"); 
      } 

      return textPane; 
     } 


//CORRECT THE SORTING FUNCTION 
// THE SORTING SHOULD BE IN REVERSE ORDER FOR YOUR REQUIREMENTS 

     // method to sort array 
     public void grades(int grdArray[]) { 
      // sort int array 
      for (int pass = 1; pass < grdArray.length; pass++) { 
       for (int element = 0; element < grdArray.length - 1; element++) { 
        swap(grades, element, element + 1); 

       } 
      } 
      addTextToPane(); 

     } 


     // method to swap elements of array 
     public void swap(int swapArray[], int first, int second) { 
      int hold; 
      hold = swapArray[first]; 
      swapArray[first] = swapArray[second]; 
      swapArray[second] = hold; 
     } 

     // execute method at run time 
     public static void main(String args[]) { 
      JFrame.setDefaultLookAndFeelDecorated(true); 
      AverageGrades f = new AverageGrades(); 
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      // accept first grade 
      int integerInput = Integer.parseInt(JOptionPane.showInputDialog(null, 
        "Please enter a grade (0-100) or -1 to calculate the average")); 

      // while loop accepts more grades, keeps count, and calulates the total 
      int count = 0; 
      grades[0] = integerInput; 
      count++; 
      int num = 0; 
      while (count < 50 && num != -1) { 
       num = Integer.parseInt(JOptionPane.showInputDialog(null, 
         "Please enter a grade (0-100) or -1 to calculate the average" 
           + (count + 1))); 

//USING TOTAL FIELD TO CALCULATE SUM 
       if (num != -1) { 
        grades[count] = num; 
        total += grades[count]; 
       } 



       count++; 

      } 

//CALL THE SORTING FUNCTION AFTER CORRECTING IT 

      //f.grades(grades); 

      // create content pane 
      f.setContentPane(f.createContentPane()); 
      f.setSize(600, 375); 
      f.setVisible(true); 

     } 
    } 
+0

謝謝@chettyharish我真的很感謝你的幫助,現在我所輸入的所有分數都會出現。然而,在分數下面,我仍然得到一長串不應該在那裏的0。相反,在最後一個分數下,它應該產生一條表示「平均值爲___」的線,並在___所在的位置插入平均值。由於某種原因,它沒有列出從最低到最高的分數 – user3053052

+0

在哪個位置計算平均值?我沒有看到它的任何地方。而長列表中的零,是由於數組有50個元素,所有未使用的值將是0 – chettyharish

+0

我在CAPS的代碼中添加了一些註釋,用它們作爲指導來糾正你的錯誤。我也解決了其中的一些問題,以方便您。 – chettyharish

1

您要定義gradearray兩次:一次裏面您class

static int[] grades = new int[50]; 

然後在main()

int[] grades = new int[50]; 
+0

添加到這一點,你需要給的contentPane你的成績數組:'f.createContentPane(等級)' – Populus

+0

謝謝現在brokenfoot它至少說明考試成績我輸入,但我仍然得到在分數和平均數仍然沒有顯示之後,還有一長串0。非常感謝Populus。如果可以的話,請準確告訴我你的意思是真棒我還是Java的新手 – user3053052

+0

Populus我明白你的意思,但是在試圖給出我的成績數組後,它說我不能,因爲它是一個整數 – user3053052