1 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
解決方案不是唯一的,但resposte是取決于。
如果你想用 numbar 給出點(diǎn)擊的順序,我認(rèn)為使用 List 會(huì)更好。
如果你只想給出總和的結(jié)果,最好使用 unique veriable。
一個(gè)示例解決方案,假設(shè)我們使用具有全局可見性的組件(雖然面向?qū)ο缶幊滩煌扑]這些具有全局可見性的組件)
public class SingletonGame{
private static final SingletonGame SINGLETON = new SingletonGame();
public static SingletonGame getInstance(){
return SINGLETON;
}
private int sum;
//or
private List<Integer> operations = new ArrayList<>();
private SingletonGame(){}
public int getSum(){
return sum;
}
//or
public List<Integer> getOperations(){
return operations;
}
public void addSum(int num){
this.sum += num;
}
//or
public void addOp(int num){
this.operations.add(num);
}
publi void getResult(){
//inside this method you have your logic
//for calculate result
}
}
//Inside your action
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==finish)
JOptionPane.showMessageDialog(null, "You made " + wrongs + "correct tries.");
else{
Square sqr = (Square) e.getSource();
int num = sqr.getNum();
// you must do control when is case for call SingletonGame.getInstance().getResult();
SingletonGame.getInstance().sum(num);
}
}
}
添加回答
舉報(bào)