问题
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:" +);
}
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.
回答1:
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...
- 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 linemyButton.addActionListener(this);
for each of your buttons. You'll probably also need to change yourpublic class MyClass
line to saypublic class MyClass implements ActionListener
, and add a lineimport java.awt.event.*
- I changed your
analyzeButtonActionPerformed()
method so that it outputs the values you requested. Now you need to implement thecalculate()
methods at the end - I have done one of them for you to give you the general idea. When you click theanalyze
button, it should do all the 4 calculations and put the answers in thetextarea
.
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.
回答2:
You have to implement actionPerformed()
method and check for corresponding button clicked.
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
}
}
来源:https://stackoverflow.com/questions/10779228/marks-program-calculations