0

我试图制作一个程序来计算我的平均值。但我只想使用一个 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';
    }    
}
4

1 回答 1

1

您已将变量定义为方法 'actionPerformed' 的局部变量。每次调用方法时都会初始化局部变量。您可以将它们定义为全局变量来解决。 在这里阅读更多

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

}

}

类 Math1 { public static double calcAverage(双 mp1,双 mp2,双考试 1,双 mp3,双 mp4,双考试 2){ 双 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';
}

}

于 2013-05-07T02:47:02.880 回答