我正在创建具有 pin 功能的 ATM 机。问题是我想在用户按下按钮时从按钮获取输入并验证它是正确还是错误。当按下按钮时,它将结果存储在字符串中。然后它将用于验证其正确或错误。例如:用户 A 按下 012345。然后每个数字将被存储到另一个字符串以进行验证。然后将字符串与引脚进行比较。
公共类atmMachine:
int numberPinButton = 10;
String pin = "012345";
String zero, one, two, three, four, five, six, seven, eight, nine;
公共自动取款机:
panel = new JPanel();
pinButton = new JButton[numberPinButton];
for(int i = 0; i < numberPinButton; i++) {
pinButton[i] = new JButton("" + i);
pinButton[i].addActionListener(this);
panel.add(pinButton[i]);
}
enterButton = new JButton("Enter");
enterButton.addActionListener(this);
panel.add(enterBtn);
panel.setBorder(BorderFactory.createTitledBorder("Enter your pin:"));
add(panel, BorderLayout.WEST);
公共无效动作执行:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == pinButton[0]) {
zero = "0";
} else if(e.getSource() == pinButton[1]) {
one = "1";
} else if(e.getSource() == pinButton[2]) {
two = "2";
} else if(e.getSource() == pinButton[3]) {
three = "3";
} else if(e.getSource() == pinButton[4]) {
four = "4";;
} else if(e.getSource() == pinButton[5]) {
five = "5";
} else if(e.getSource() == pinButton[6]) {
six = "6";
} else if(e.getSource() == pinButton[7]) {
seven = "7";
} else if(e.getSource() == pinButton[8]) {
eight = "8";
} else if(e.getSource() == pinButton[9]) {
nine = "9";
}
if(e.getSource() == enterBtn) {
if(???.equals(pin)) {
System.out.println("Correct");
} else {
System.out.println("Wrong");
}
}
}