請考慮下面的代碼:陣列和if語句:Array對象才能通行進入if語句
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語句覆蓋?
使用'String#equals'來比較字符串。 '=='比較對象引用 – Reimeus
Luiggi,我看到你指出可能的重複,但我從來沒有發現或者甚至想過詢問關於字符串比較的問題。 – obious
@obious - 這裏的一個愚蠢的原因是(比較流行)關於比較Java中的String的問題是實際的問題/錯誤,對這個問題的任何答案都只是相同的答案。 –