0

我正在制作一个简单的程序,用于检查用户针对数组中的某些值输入的内容。当我运行程序时它没有错误,但是当我单击提交按钮时它不会执行指定的操作。有人可以告诉我如何解决这个问题吗?

import javax.swing.*;
import java.awt.event.*;

public class Program1 extends JFrame {

    private JTextField textfield;

    private JButton submitButton;

    int convertedInputScore; 

    String inputScore;

    int[] studentScore = {-1, 40, 50, 60, 70, 80, 100, 101};

    public Program1() {

        this.setSize(600, 250);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Student mark checker");

        JPanel panel = new JPanel();

        JLabel label = new JLabel("Type in student mark between 0 and 100, or -1 to end");

        textfield = new JTextField(45);

        submitButton = new JButton("Submit");
        submitButton.addActionListener(clicklistener);

        this.add(panel);
        panel.add(label);
        panel.add(textfield);
        panel.add(submitButton);
        setVisible(true);


    }

    ClickListener clicklistener = new ClickListener();

    private class ClickListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == submitButton) {

                String inputScore = textfield.getText();
                int convertedInputScore = Integer.parseInt(inputScore);

                checkInputScore();

            }
        }
    }

    public void checkInputScore() {

        if (convertedInputScore == studentScore[0]) {
            System.exit(0);
        } 

        if (convertedInputScore == studentScore[1]) {
        }
    }

    public static void main(String[] args) {

        new Program1();

    }
}
4

1 回答 1

3

convertedInputScore当您似乎只想为其分配新值时,您在动作侦听器中再次声明。改变这个:

int convertedInputScore = Integer.parseInt(inputScore);

对此:

convertedInputScore = Integer.parseInt(inputScore);
于 2012-08-06T01:31:27.187 回答