我正在尝试重构代码-为此,我想为这些布尔值创建一个循环/数组:
boolean _1U() { return secondClicked() == buttons[fieldZero() - 1][fieldOne()]; }
boolean _1D() { return secondClicked() == buttons[fieldZero() + 1][fieldOne()]; }
boolean _1R() { return secondClicked() == buttons[fieldZero()][fieldOne() + 1]; }
boolean _1L() { return secondClicked() == buttons[fieldZero()][fieldOne() - 1]; }
boolean _1U1R() { return secondClicked() == buttons[fieldZero() - 1][fieldOne() + 1]; }
boolean _1U1L() { return secondClicked() == buttons[fieldZero() - 1][fieldOne() - 1]; }
boolean _1D1R() { return secondClicked() == buttons[fieldZero() + 1][fieldOne() + 1]; }
boolean _1D1L() { return secondClicked() == buttons[fieldZero() + 1][fieldOne() - 1]; }
关于方法/按钮:
JButton[][] buttons = new JButton[12][12];
JTextField[] textField = new JTextField[4];
private JButton secondClicked() { return buttons[fieldTwo()][fieldThree()]; }
private int fieldZero() { return Integer.parseInt(textField[0].getText()); }
private int fieldOne() { return Integer.parseInt(textField[1].getText()); }
int fieldTwo() { return Integer.parseInt(textField[2].getText()); }
int fieldThree() { return Integer.parseInt(textField[3].getText()); }
我使用布尔值:
void moveBlackPawn() {
if (_1D1R() && !iNull() || _1D1L() && !iNull() || _1D() && iNull()) {
doMove();
}
}
void moveWhitePawn() {
if (_1U1R() && !iNull() || _1U1L() && !iNull() || _1U() && iNull()) {
doMove();
}
}
我试图将按钮放入数组(以进行测试),但最终得到了NPE。
boolean[] direction = {secondClicked() == buttons[fieldZero() - 1][fieldOne()], secondClicked() == buttons[fieldZero() + 1][fieldOne()]};
void moveBlackPawn() {
if (_1D1R() && !iNull() || _1D1L() && !iNull() || direction[1] && iNull()) {
doMove();
}
}
void moveWhitePawn() {
if (_1U1R() && !iNull() || _1U1L() && !iNull() || direction[0] && iNull()) {
doMove();
}
}
有什么方法可以循环这些/使数组工作吗?