gpt4 book ai didi

java - Java中使用一个TextField输入多个数据

转载 作者:行者123 更新时间:2023-12-01 14:36:53 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来计算我的平均值。但我只想使用一个 TextField 进行输入。有什么建议吗?

我试图做到这一点,以便当按钮更改时,它将数字分配给变量。但似乎不起作用。

此外,Java 让我将变量分配为 0...我不知道为什么...但无论我手动分配给变量什么,都是用于计算最终成绩的。

这就是我到目前为止所拥有的......

public class FinalApplet extends Applet implements ActionListener
{
private TextField gradeField, finalField;
private Button enter;

public void init()
{
gradeField = new TextField(8);
gradeField.addActionListener(this);
add(gradeField);

enter = new Button("Enter MP1 Grade");
enter.addActionListener(this);
add(enter);

finalField = new TextField(8);
finalField.addActionListener(this);
add(finalField);
}

public void actionPerformed(ActionEvent e)
{
double mp1 = 0;
double mp2 = 0;
double exam1 = 0;
double mp3 = 0;
double mp4 = 0;
double exam2 = 0;
double finalGrade = 0;
char letterGrade;
//if (e.getSource() == gradeField || e.getSource() == enter)

if (enter.getLabel() == "Enter MP1 Grade")
{
mp1 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP2 Grade");
}
else if (enter.getLabel() == "Enter MP2 Grade")
{
mp2 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter Exam1 Grade");
}
else if (enter.getLabel() == "Enter Exam1 Grade")
{
exam1 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP3 Grade");
}
else if (enter.getLabel() == "Enter MP3 Grade")
{
mp3 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP4 Grade");
}
else if (enter.getLabel() == "Enter MP4 Grade")
{
mp4 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter Exam2 Grade");
}
else if (enter.getLabel() == "Enter Exam2 Grade")
{
exam2 = Double.parseDouble(gradeField.getText());
enter.setLabel("Calculate Final Grade");
}
else if (enter.getLabel() == "Calculate Final Grade")
{
finalGrade = Math1.calcAverage(mp1, mp2, exam1, mp3, mp4, exam2);
letterGrade = Math1.getLetter(finalGrade);
finalField.setText(letterGrade + " - " + finalGrade);
enter.setLabel("Enter MP1 Grade");
}

}
}

public class Math1
{
public static double calcAverage(double mp1, double mp2, double exam1, double mp3, double mp4, double exam2)
{
double finalGrade;

finalGrade = (mp1 * .2) + (mp2 * .2) + (exam1 * .1) + (mp3 * .2) + (mp4 * .2) + (exam2 * .1);

return finalGrade;
}

public static char getLetter(double finalGrade)
{
if (finalGrade >= 89.5)
return 'A';
else if (finalGrade >= 79.5 && finalGrade < 89.5)
return 'B';
else if (finalGrade >= 69.5 && finalGrade < 79.5)
return 'C';
else if (finalGrade >= 59.5 && finalGrade < 69.5)
return 'D';
else
return 'F';
}
}

最佳答案

您已将变量定义为方法 'actionPerformed' 的局部变量。每次调用方法时都会初始化局部变量。您可以将它们定义为全局变量来求解。 Read More here

package self.edu.stackoverflow;

import java.applet.Applet;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StaticFinal extends Applet implements ActionListener {
private TextField gradeField, finalField;
private Button enter;

double mp1 = 0;
double mp2 = 0;
double exam1 = 0;
double mp3 = 0;
double mp4 = 0;
double exam2 = 0;
double finalGrade = 0;
char letterGrade;

public void init() {
gradeField = new TextField(8);
gradeField.addActionListener(this);
add(gradeField);

enter = new Button("Enter MP1 Grade");
enter.addActionListener(this);
add(enter);

finalField = new TextField(8);
finalField.addActionListener(this);
add(finalField);
}

public void actionPerformed(ActionEvent e) {

// if (e.getSource() == gradeField || e.getSource() == enter)

if (enter.getLabel() == "Enter MP1 Grade") {
mp1 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP2 Grade");
} else if (enter.getLabel() == "Enter MP2 Grade") {
mp2 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter Exam1 Grade");
} else if (enter.getLabel() == "Enter Exam1 Grade") {
exam1 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP3 Grade");
} else if (enter.getLabel() == "Enter MP3 Grade") {
mp3 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter MP4 Grade");
} else if (enter.getLabel() == "Enter MP4 Grade") {
mp4 = Double.parseDouble(gradeField.getText());
enter.setLabel("Enter Exam2 Grade");
} else if (enter.getLabel() == "Enter Exam2 Grade") {
exam2 = Double.parseDouble(gradeField.getText());
enter.setLabel("Calculate Final Grade");
} else if (enter.getLabel() == "Calculate Final Grade") {
finalGrade = Math1.calcAverage(mp1, mp2, exam1, mp3, mp4, exam2);
letterGrade = Math1.getLetter(finalGrade);
finalField.setText(letterGrade + " - " + finalGrade);
enter.setLabel("Enter MP1 Grade");
}

}

}

数学1类{ 公共(public)静态双 calcAverage(双 mp1, 双 mp2, 双 exam1, 双 mp3、双 mp4、双考试2) { 双最终成绩;

    finalGrade = (mp1 * .2) + (mp2 * .2) + (exam1 * .1) + (mp3 * .2)
+ (mp4 * .2) + (exam2 * .1);

return finalGrade;
}

public static char getLetter(double finalGrade) {
if (finalGrade >= 89.5)
return 'A';
else if (finalGrade >= 79.5 && finalGrade < 89.5)
return 'B';
else if (finalGrade >= 69.5 && finalGrade < 79.5)
return 'C';
else if (finalGrade >= 59.5 && finalGrade < 69.5)
return 'D';
else
return 'F';
}

}

关于java - Java中使用一个TextField输入多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com