-1

我必须制作一个标记程序,显示用户输入的一组标记和一些计算(平均值、最大值、最小值、范围等)。它还应该能够按升序对标记进行排序。我设法(在帮助下)显示标记并对它们进行排序,但我无法让程序进行计算并显示它们。这是我到目前为止的所有代码:

 ArrayList <Integer> marks = new ArrayList();

 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.addAll(marks, (Integer.parseInt(markInput.getText())));

    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
    text.append(mark.toString()).append('\n');
    }

    markdisplayTextArea.setText(text.toString());
    }

 private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Collections.sort(marks);

    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
    text.append(mark.toString()).append('\n');
    }

    markdisplayTextArea.setText(text.toString());
    }

 private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {

    analyzeTextArea.setText("Class average:" +);
    analyzeTextArea.setText("Maximum mark:" +);
    analyzeTextArea.setText("Minimum mark:" +);
    analyzeTextArea.setText("Range of marks:" +);
    }

按下按钮TextArea时,计算必须显示在 a中。"analyze"我目前不知道如何进行计算或显示它们。任何指导将不胜感激。

4

2 回答 2

2

您必须实现actionPerformed()方法并检查是否单击了相应的按钮。

 public void actionPerformed(ActionEvent e) {
        if(e.getSource()==addButton) {
                        //do stuff
        }
        else if(e.getSource()==sortButton) {
              // do stuff
        }
        else if(e.getSource()==analyzeButton) {
              // do stuff
        }
    }
于 2012-05-28T04:17:06.000 回答
2

这是我为您的问题建议的代码...

ArrayList<Integer> marks = new ArrayList<Integer>();

// Called when the user clicks a button
public void actionPerformed(ActionEvent e) {
    Object clickedButton = e.getSource();
    if(clickedButton == addButton) {
        addButtonActionPerformed();
    }
    else if(clickedButton == sortButton) {
        sortButtonActionPerformed();
    }
    else if(clickedButton == analyzeButton) {
        analyzeButtonActionPerformed();
    }
}

 private void addButtonActionPerformed() {
    Collections.addAll(marks, (Integer.parseInt(markInput.getText())));

    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
        text.append(mark.toString()).append('\n');
    }

    markdisplayTextArea.setText(text.toString());
}

 private void sortButtonActionPerformed() {
    Collections.sort(marks);

    StringBuilder text = new StringBuilder();
    for (Integer mark: marks) {
        text.append(mark.toString()).append('\n');
    }

    markdisplayTextArea.setText(text.toString());
}

private void analyzeButtonActionPerformed() {

    String output = "Class average:" + calculateAverage() + "\n" +
                    "Maximum mark:" + calculateMaximum() + "\n" +
                    "Minimum mark:" + calcualteMinimum() + "\n" +
                    "Range of marks:" + calculateRange();

    analyzeTextArea.setText(output);
}

private int calculateAverage(){
    // calculate and return the answer
}

private int calculateMaximum(){
    // calculate and return the answer
    int maximum = 0;
    for (Integer currentMark: marks){
        if (currentMark > maximum){
            maximum = currentMark;
        }
    }
    return maximum;
}

private int calcualteMinimum(){
    // calculate and return the answer
}

private int calculateRange(){
    // calculate and return the answer
}

现在,这些是我的主要更改,以及我做出这些更改的原因...

  1. 我添加了一个actionPerformed()方法。如果你还没有这个,它可以让你监听按钮点击。您需要myButton.addActionListener(this);为每个按钮添加一行。您可能还需要将您的public class MyClass行更改为 saypublic class MyClass implements ActionListener并添加一行import java.awt.event.*
  2. 我更改了您的analyzeButtonActionPerformed()方法,以便它输出您请求的值。现在您需要calculate()在最后实现这些方法 - 我已经为您完成了其中一个,以便为您提供总体思路。当您单击analyze按钮时,它应该完成所有 4 次计算并将答案放入textarea.

Why don't you have a bit of a look at my changes, and see whether you can see what they're supposed to be doing. Have a go at implementing some of the changes, and the calculate methods, and see if you can get it working. Don't just copy-paste my answer - make some changes bit-by-bit to see how it all works, and fix any compilation errors as they appear.

于 2012-05-28T13:46:18.693 回答