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

l2osamch  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(329)

编程新手,所以请放松。
我正在为课堂准备一个计算器应用程序。我要设置加法、减法、乘法和除法。

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\u keypressed,我试图得到1+1=2,而不是1+1=11。所以,我试图保持输入的值,同时添加新的值。这听起来像是在传递参数(我可能错了),或者是在建模,但我不太确定。我不知道如何在没有静态的情况下做到这一点(这在我的课堂上是绝对禁止的),从我看到的其他人所做的,这是标准。此外,在所有这些中,还有一个方法签名-
公共int perform(int arg)
必须包括在内。
提前谢谢。

nwsw7zdq

nwsw7zdq1#

以下是更改列表
1.为运算符添加枚举
2.向类中添加运算符变量
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;
    }
}

相关问题