Javascript计算器,输入新号码时如何清除以前的号码?

时间:2017-12-09 23:00:27

标签: javascript calculator

我正在使用javascript编写计算器并遇到问题。

输入新号码时,我无法找到清除显示的代码。

实施例

  • 按3(屏幕上3 apear)

  • 按+ / - / * /(将3保存到num1并设置操作,然后屏幕清除。)

  • 按4(屏幕上的4 apear)

  • 按另一个+(将4保存到num2,解决(num1 + num2),然后将答案保存回num1)

- 这是我无法弄清楚输入新号码时如何清除显示的问题。在系统解决了问题后,它将答案放在显示屏上,当我输入一个新的数字时,它会与答案叠加。假设答案是45,我要减去56,输入数字显示将显示4556。

希望计算器像

  • 按52(52出现)
  • 按+ / - / * /(52仍在显示屏中,操作在后台设置)
  • 按42(52开出并替换为42)
  • 按+(显示屏上显示答案并保持不变)
  • 输入另一个号码(答案清除,屏幕显示新号码)

如何清除显示屏上的答案以输入新号码。

var num1;

var num2;

var operation;

function numPress(num){
    var current = document.getElementById("display").value;
    current = current + num;
    document.getElementById("display").value = current;
}

function opPress(opcode){
    var current = document.getElementById("display").value;
    current = parseInt(document.getElementById("display").value);
        if( num1== null){
        num1 = current;
        operation= opcode;
        document.getElementById("display").value = null;
    }
        else{
            num2 = current;
                if (operation == "+" ){
                    var equal = ( num1 + num2 );
                    num1 = equal;
                    operation= opcode;
                    document.getElementById("display").value = num1;
                }
                if (operation == "-" ){
                      var equal = ( num1 - num2 );
                      num1 = equal;
                      operation= opcode;
                      document.getElementById("display").value = num1;
                }
                if (operation == "*" ){
                      var equal = ( num1 * num2 );
                      num1 = equal;
                      operation= opcode;
                      document.getElementById("display").value = num1;
                }
                if (operation == "/" ){
                      var equal = ( num1 / num2 );
                      num1 = equal;
                      operation= opcode;
                      document.getElementById("display").value = num1;
                }
          }
    }

    function calculation(){
      var current = document.getElementById("display").value;
      current = parseInt(document.getElementById("display").value);
      num2 = current;
          if (operation == "+" ){
                var equal = ( num1 + num2 );
                document.getElementById("display").value = equal;
          }
          if (operation == "-" ){
                var equal = ( num1 - num2 );
                document.getElementById("display").value = equal;
          }
          if (operation == "*" ){
                var equal = ( num1 * num2 );
                document.getElementById("display").value = equal;
          }
          if (operation == "/" ){
                var equal = ( num1 / num2 );
                document.getElementById("display").value = equal;
          }
        }

function calculation(){ 
    var current= document.getElementById("display").value;
    num1 = null;
    num2 = null;
    var no = null;
    document.getElementById("display").value = no;
}
<!DOCTYPE html>
    <html>
        <head>
            <title>Calculator</title>
                <link rel="stylesheet" href="style.css">
        </head>

  <body>
<table>
  <tr>
    <input id="display" type ="text" value="" onkeydown="return false;">
  </tr>
  <tr>
    <td></td><td></td><td></td>
    <td colspan="5"><input id="c" type="button" value = "DEL" onclick = "cls();"></td>
  </tr>
  <tr>
    <td></td><td></td><td></td>
    <td><input id="b" type="button" value = "7" onclick = "numPress('7');"></td>
    <td><input id="b" type="button" value = "8" onclick = "numPress('8');"></td>
    <td><input id="b" type="button" value = "9" onclick = "numPress('9');"></td>
    <td><input id="a" type="button" value = "+" onclick = "opPress('+');"></td>
    <td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td>
    <td><input id="b" type="button" value = "4" onclick = "numPress('4');"></td>
    <td><input id="b" type="button" value = "5" onclick = "numPress('5');"></td>
    <td><input id="b" type="button" value = "6" onclick = "numPress('6');"></td>
    <td><input id="a" type="button" value = "-" onclick = "opPress('-');"></td>
    <td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td>
    <td><input id="b" type="button" value = "1" onclick = "numPress('1');"></td>
    <td><input id="b" type="button" value = "2" onclick = "numPress('2');"></td>
    <td><input id="b" type="button" value = "3" onclick = "numPress('3');"></td>
    <td><input id="a" type="button" value = "*" onclick = "opPress('*')"></td>
    <td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td>
    <td><input id="b" type="button" value = "0" onclick = "numPress('0');"></td>
    <td><input id="a1" type="button" value = "." onclick = "numPress('.');"></td>
    <td><input id="a2" type="button" value = "=" onclick = "calculation('=');"></td>
    <td><input id="a" type="button" value = "/" onclick = "opPress('/');"></td>
    <td></td><td></td><td></td>
  </tr>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以添加另一个名为var的{​​{1}}。

lastPress内设置calculation()。 在lastPress = calculationnumPress()内,您可以将其设置为其他内容。

然后在opPress()内检查最后一次按下是否为numPress(),如果是这样,请将其清除。

如果有人计算了一个数字(它将显示在屏幕上),那么他们所做的下一件事就是输入一个数字,它应该清除屏幕。我相信这是你想要实现的目标。

如果你想要稍微不同的结果,你可以修改lastPress的位置/逻辑。