请考虑以下代码:
public JButton math_button[] = new JButton[5];
for (int h = 0; h <math_button.length; h++) {
if(event.getSource()==math_button[h]) {
String button_press = math_button[h].getText();
if(math_button[h].getText().equals("Equals")) {
secondn = Integer.parseInt(math_input.getText());
System.out.println(firstn + " math operator " + secondn + " and "+ math_button[h].getText());
System.out.println(calc.Math(button_press, firstn, secondn));
} else {
firstn = Integer.parseInt(math_input.getText());
//math_input.setText("");
//placeholder = calc.Math(math_button[h].getText(), firstn, secondn);
//int secondn = Integer.parseInt(math_input.getText());
//int result = calc.Math(math_button[h].getText(), firstn, secondn);
//math_input.setText(Integer.toString(firstn));
//math_input.setText(Integer.toString(placeholder));
}
}
}
尽管变量button_press
被设置为第二个 IF(嵌套)循环之外的数组对象的名称,但测试条件变量math_button[h].getText()
始终传递给calc.Math
方法的原因是什么?
字符串的变量是否button_press
被嵌套的 IF 语句覆盖?