我正在创建一个简单的程序,用于检查用户对数组中某些值的输入。当我运行程序时它没有错误,但是当我单击提交按钮时它不会执行指定的操作。有人能告诉我如何解决这个问题吗?
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();
}
}
答案 0 :(得分:3)
您在动作侦听器中再次声明convertedInputScore
,而您似乎只想为其分配新值。改变这个:
int convertedInputScore = Integer.parseInt(inputScore);
对此:
convertedInputScore = Integer.parseInt(inputScore);