1 回答
TA贡献1875条经验 获得超5个赞
解决方案不是唯一的,但resposte是取决于。
如果你想用 numbar 给出点击的顺序,我认为使用 List 会更好。
如果你只想给出总和的结果,最好使用 unique veriable。
一个示例解决方案,假设我们使用具有全局可见性的组件(虽然面向对象编程不推荐这些具有全局可见性的组件)
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);
}
}
}
添加回答
举报