OK, es ist also kein Problem innerhalb der Aktionsmethode, aber wenn es auf dem neuen Frame angezeigt wird. Hier ist der neue Code, der die Anzahl und die durchschnittliche Punktzahl zweimal anzeigt, einmal auf dem ersten Bild (die korrekten Werte) und dann auf dem zweiten (es wurde aus einem unbekannten Grund auf 0 zurückgesetzt).Java var kehrt zurück zu 0, wenn es in einem neuen Frame angezeigt wird
package testing2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Testing2 extends JFrame implements ActionListener {
private double score;
protected double totalScore;
protected double averageScore;
protected int counter = 0;
JButton next = new JButton("Next");
JButton display = new JButton("Display");
JTextField scoreField = new JTextField("0", 3);
JTextField commentField = new JTextField(30);
JLabel explainationLabel = new JLabel("Please Score your overall saticfaction with our app");
JLabel explainationLabel2 = new JLabel("Enter any comments or suggestions");
JLabel testDisplay = new JLabel();
public Testing2() {
super("App Survey");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(explainationLabel);
add(scoreField);
add(explainationLabel2);
add(commentField);
add(next);
add(display);
add(testDisplay);
next.addActionListener(this);
display.addActionListener(this);
}
public Testing2 (String t){
super(t);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == next) {
score = Double.parseDouble(scoreField.getText());
if(score < 11 && score > 0)
{
totalScore += score;
counter ++;
scoreField.setText("0");
commentField.setText("");
}
else
JOptionPane.showMessageDialog(null, "Please enter a score bettween 1 and 10", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
averageScore = totalScore/counter;
testDisplay.setText("The average score is: " + averageScore + " out of " + counter + " votes.");
viewLoginFrame();
}
}
public static void main(String[] args) {
Testing2 testFrame = new Testing2();
final int WIDTH = 375;
final int HEIGHT = 200;
testFrame.setSize(WIDTH, HEIGHT);
testFrame.setVisible(true);
}
public void viewLoginFrame() {
secondframe loginFrame = new secondframe();
final int WIDTH = 300;
final int HEIGHT = 200;
loginFrame.setSize(WIDTH, HEIGHT);
loginFrame.setVisible(true);
}
}
package testing2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class secondframe extends Testing2 {
JLabel displayScore = new JLabel();
JLabel displayScore2 = new JLabel();
JLabel displayCommentCounter = new JLabel();
public secondframe() {
super("Survey Results");
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(displayScore);
add(displayScore2);
add(displayCommentCounter);
displayScore.setText("The average overall saticfaction");
displayScore2.setText("score for the day is: " + averageScore);
displayCommentCounter.setText("The total number of Surveys taken was: " + counter);
}
}
Wie sind Sie zu dem Schluss gekommen, dass der Matheteil nicht funktioniert? – sascha10000
Willst du sagen, dass der Block 'if (score <11 && score> 0)' eingegeben wurde, aber der Zähler nicht inkrementiert wird? Was ist "Kommentare" wo ist es definiert? Sind Sie sicher, dass dieser Code ausgeführt wird? Hast du es satt, es zu debuggen? –
Es sollte nach dem Drücken der Ergebnistaste in einem anderen Rahmen angezeigt werden. Mit diesem Code wird 0 sowohl für durationScore als auch für surveyCounter angezeigt. Aber wenn ich surveyCounter = auf 5 setze (wenn die Variable deklariert ist), wird 5 angezeigt für surveyCounter –