我有一个家庭作业,我一直在编码。我以为我已经完成了,但每当我想显示平均值时,它会在内容窗格中显示一个 0 列表。
这是任务的描述。
编写一个 Swing 程序,声明一个最大长度为 50 的空成绩数组。在 while 循环中实现一个 JOptionPane 输入框,以允许用户输入成绩。当用户输入 -1 的标记值时,这将表示数据输入循环的结束。
输入成绩后,内容窗格应显示从最低到最高排序的成绩。编写一个循环遍历数组,查找大于零 (0) 的元素。保持这些项目的运行计数,并将它们累积到一个总数中。将总计除以输入的成绩数以找到平均值,并将平均值显示在排序的成绩列表的末尾。使用 DecimalFormat 方法将平均值显示为小数点后 2 位。
/*
Chapter 7: Average of grades
Programmer:
Date:
Filename: Averages.java
Purpose: To use the Java Swing interface to calculate the average of up to 50 grades.
Average is calculated once -1 is entered as a value. The grades are then sorted
from lowest to highest and displayed in a content pane which also displayes the average.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
public class Averages 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);
Averages f = new Averages();
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);
}
}