马克斯程序计算? [英] Marks Program Calculations?

查看:93
本文介绍了马克斯程序计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不做出这样显示一组标记,用户输入和一些计算(平均值,最大值,最小值,范围等)标记程序。它也应当能够以升序标记排序。我管理(的帮助),以显示标记和排序他们,但我不能让程序做计算并显示出来。这是所有的code我到目前为止有:

I have to make a marks program that displays a set of marks that a user enters and some calculations (average, maximum, minimum, range, etc.). It should also be able to sort the marks in ascending order. I managed (with help) to display the marks and sort them but I cannot get the program to do the calculations and display them. This is all the code I have so far:

 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:" +);
    }

的计算必须显示在文本区分析按钮为pressed。我现在不知道该怎样去这样做计算或显示他们的想法。任何指导,将AP preciated。

The calculations must be displayed in a TextArea when the "analyze" button is pressed. I currently have no idea of how to go about doing the calculations or displaying them. Any guidance would be appreciated.

推荐答案

下面是我建议的code您的问题...

Here is my suggested code for your problem...

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
}

现在,这些都是我的主要变化,为什么我让他们...

Now, these are my main changes, and why I made them...


  1. 我添加了一个的actionPerformed()方法。如果你没有这个已经,它可以让你监听按钮点击。您需要添加一行 myButton.addActionListener(本); 为每个按钮。你可能还需要改变你的公共MyClass类网​​上说的公共MyClass类实现的ActionListener ,并添加行进口java.awt.event中。*

  2. 我改变了你的 analyzeButtonActionPerformed()方法使其输出您所要求的数值。现在,你需要实现计算()末方法 - 我已经做了其中的一个给你给你的总体思路。当您单击分析按钮,它应该做的所有的4计算,并把答案在的textarea

  1. I added a actionPerformed() method. If you don't have this already, it allows you to listen for button clicks. You need to add a line myButton.addActionListener(this); for each of your buttons. You'll probably also need to change your public class MyClass line to say public class MyClass implements ActionListener, and add a line import java.awt.event.*
  2. I changed your analyzeButtonActionPerformed() method so that it outputs the values you requested. Now you need to implement the calculate() methods at the end - I have done one of them for you to give you the general idea. When you click the analyze button, it should do all the 4 calculations and put the answers in the 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.

这篇关于马克斯程序计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆