如何在 Java 中保留和添加另一个变量?

How To Keep and Add Another Variable In Java?

编程新手,所以请放轻松。

我正在为 class 开发计算器应用程序。我必须设置加法、减法、乘法和除法。

package part1.code;

import part1.user_interface.CalculatorUI;

public class Calculator {

private static final int BASE = 10;
private int _value;
private int _va;
private CalculatorUI _ui;

public Calculator(CalculatorUI calculatorUI) {
    _ui = calculatorUI;
    _value = 0;

}

public void digit_0_keyPressed() {
    _value = _value * BASE + 0;
    _ui.updateDisplay();
}
public void digit_1_keyPressed() {
    _value = _value * BASE + 1;
    _ui.updateDisplay();
}
public void digit_2_keyPressed() {
    _value = _value * BASE + 2;
    _ui.updateDisplay();
}
public void digit_3_keyPressed() {
    _value = _value * BASE + 3;
    _ui.updateDisplay();
}
public void digit_4_keyPressed() {
    _value = _value * BASE + 4;
    _ui.updateDisplay();
}
public void digit_5_keyPressed() {
    _value = _value * BASE + 5;
    _ui.updateDisplay();
}
public void digit_6_keyPressed() {
    _value = _value * BASE + 6;
    _ui.updateDisplay();
}
public void digit_7_keyPressed() {
    _value = _value * BASE + 7;
    _ui.updateDisplay();
}
public void digit_8_keyPressed() {
    _value = _value * BASE + 8;
    _ui.updateDisplay();
}
public void digit_9_keyPressed() {
    _value = _value * BASE + 9;
    _ui.updateDisplay();
}
public void clear_keyPressed() {
    _value = 0;
    _ui.updateDisplay();
}
public int getValue() {
    return _value;
}

public void equal_keyPressed() {
    _value = _value;
    _ui.updateDisplay();

    // TODO Define this method
}

public void add_keyPressed() {
    _value = 



    _ui.updateDisplay();

    // TODO Define this method

  }
}

现在,例如,您要做的基本事情是 1+1。对于 add_keyPressed,我试图达到 1+1 = 2,而不是 1+1 = 11。因此,我试图保留值输入,并同时添加新值。这听起来像是我在传递参数(我可能错了)或建模,但我不太确定。如果没有静态(这在我的 class ),从我看到其他人所做的来看,这是标准。 此外,在所有这些中,方法签名 -

public int 执行(int arg)

必须包括在内。

提前致谢。

这是更改列表
1. 为 operator
添加枚举 2. 在class
中添加一个运算符变量 3.更改构造函数以设置运算符变量的默认值
4.更新等于按键方法
5.更新添加按键方法

public class Calculator {

    private static final int BASE = 10;
    private int _value;
    private int _va;
    private CalculatorUI _ui;
    private Operator op;

    private enum Operator {
        ADD, SUB, MUL, DIV, NO_OPERATION
    };

    public Calculator(CalculatorUI calculatorUI) {
        op = Operator.NO_OPERATION;
        _ui = calculatorUI;
        _value = 0;
    }

    public void digit_0_keyPressed() {
        _value = _value * BASE + 0;
        _ui.updateDisplay();
    }

    public void digit_1_keyPressed() {
        _value = _value * BASE + 1;
        _ui.updateDisplay();
    }

    public void digit_2_keyPressed() {
        _value = _value * BASE + 2;
        _ui.updateDisplay();
    }

    public void digit_3_keyPressed() {
        _value = _value * BASE + 3;
        _ui.updateDisplay();
    }

    public void digit_4_keyPressed() {
        _value = _value * BASE + 4;
        _ui.updateDisplay();
    }

    public void digit_5_keyPressed() {
        _value = _value * BASE + 5;
        _ui.updateDisplay();
    }

    public void digit_6_keyPressed() {
        _value = _value * BASE + 6;
        _ui.updateDisplay();
    }

    public void digit_7_keyPressed() {
        _value = _value * BASE + 7;
        _ui.updateDisplay();
    }

    public void digit_8_keyPressed() {
        _value = _value * BASE + 8;
        _ui.updateDisplay();
    }

    public void digit_9_keyPressed() {
        _value = _value * BASE + 9;
        _ui.updateDisplay();
    }

    public void clear_keyPressed() {
        _value = 0;
        _ui.updateDisplay();
    }

    public int getValue() {
        return _value;
    }

    public void equal_keyPressed(String uimessage) {
        if (Operator.ADD.equals(op)) {
            _value += _va;
        } else if (Operator.MUL.equals(op)) {
            _value *= _va;
        } else if (Operator.DIV.equals(op)) {
            _value = _va / _value;
        } else if (Operator.SUB.equals(op)) {
            _value = _va - _value;
        }
        _va = 0;
        op = Operator.NO_OPERATION;
        _ui.updateDisplay();
    }

    public void add_keyPressed() {
        if (!Operator.NO_OPERATION.equals(op)) {
            equal_keyPressed();
        } else {
        _ui.updateDisplay();
        }

        _va = _value;
        _value = 0;
        op = Operator.ADD;
    }

    public void mul_keyPressed() {
        if (!Operator.NO_OPERATION.equals(op)) {
            equal_keyPressed();
        } else {
            _ui.updateDisplay();
        }

        _va = _value;
        _value = 0;
        op = Operator.MUL;
    }
}