我的代碼幾乎是完美的......我想。我需要輸入用戶輸入的測試分數(最多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);
}
}
謝謝@chettyharish我真的很感謝你的幫助,現在我所輸入的所有分數都會出現。然而,在分數下面,我仍然得到一長串不應該在那裏的0。相反,在最後一個分數下,它應該產生一條表示「平均值爲___」的線,並在___所在的位置插入平均值。由於某種原因,它沒有列出從最低到最高的分數 – user3053052
在哪個位置計算平均值?我沒有看到它的任何地方。而長列表中的零,是由於數組有50個元素,所有未使用的值將是0 – chettyharish
我在CAPS的代碼中添加了一些註釋,用它們作爲指導來糾正你的錯誤。我也解決了其中的一些問題,以方便您。 – chettyharish