简体   繁体   English

使用条件语句写一个javascript计算器

[英]Use conditional statement to write a javascript calculator

I'm new to javascript.我是 javascript 新手。 I did a simple calculator that has 2 inputs values with 4 buttons of operators.我做了一个简单的计算器,它有 2 个输入值和 4 个运算符按钮。 How can I fix this javascript so that it can count the numbers based on different operators and display the correct output?如何修复此 javascript 以便它可以根据不同的运算符计算数字并显示正确的输出? How to write it using if else condition or switch cases?如何使用 if else 条件或 switch case 编写它?

Now I have pressed every button it only shows the output with sum only.现在我按下了每个按钮,它只显示总和的输出。

 function count() { var n1 = parseFloat(document.getElementById("num1").value); var n2 = parseFloat(document.getElementById("num2").value); var optr = document.getElementById("operator").value; let result; if (optr == '+') { result = n1 + n2; } else if (optr == '-') { result = n1 - n2; } else if (optr == '*') { result = n1 * n2; } else { result = n1 / n2; } document.getElementById("output").innerHTML = "Total is: " + result; }
 Number 1:<input type="number" id="num1"><br><br> Number 2:<input type="number" id="num2"><br><br> <input type="button" value="+" onclick="count()" id="operator"> <input type="button" value="-" onclick="count()" id="operator"> <input type="button" value="*" onclick="count()" id="operator"> <input type="button" value="/" onclick="count()" id="operator"> <p id="output"></p>

There are many ways to achieve what you want.有很多方法可以实现你想要的。 Here is one that I have prepared by modifying/simplifying your original code:这是我通过修改/简化您的原始代码准备的:

 const in1 = document.getElementById("num1"), in2 = document.getElementById("num2"); document.addEventListener("click", function(ev) { if (ev.target.classList.contains("operator")) { let optr = ev.target.value, n1=+in1.value, n2=+in2.value, result; if (optr == '+') result = n1 + n2; else if (optr == '-') result = n1 - n2; else if (optr == '*') result = n1 * n2; else result = n1 / n2; document.getElementById("output").innerHTML = "Total is: " + result; } })
 Number 1:<input type="number" id="num1"><br><br> Number 2:<input type="number" id="num2"><br><br> <input type="button" value="+" class="operator"> <input type="button" value="-" class="operator"> <input type="button" value="*" class="operator"> <input type="button" value="/" class="operator"> <p id="output"></p>

A few remarks:几点说明:

  • id attributes must always be unique on a page. id属性在页面上必须始终是唯一的。 I replaced the id s in your buttons by class attributes.我用class属性替换了按钮中的id
  • the values of your input elements must be evaluated at the time the operator button is clicked.必须在单击操作员按钮时评估input元素的值。
  • the conversion from text to numerical values is done implicitly by applying the unary + operator in front of in1.value and in2.value .从文本到数值的转换是通过在in1.valuein2.value前面应用一元+运算符隐式完成的。
  • instead of assigning the handler function through the html- onclick attribute I used a delegated event attachment : the click event is attached to the whole document but will only cause an action if the actual clicked element ( ev.target ) has the word "operator" in its class list.我没有通过 html- onclick属性分配处理程序函数,而是使用了委托事件附件:单击事件附加到整个文档,但仅当实际单击的元素( ev.target )具有“operator”一词时才会导致操作在它的类列表中。

Switch case or If/else.切换大小写或 If/else。 Both is right.两者都是对的。 But I prefer the switch case version, because it is cleaner.但我更喜欢开关盒版本,因为它更干净。 Following @CarstenMassmann's answer, here is the switch case path:按照@CarstenMassmann 的回答,这里是 switch case 路径:

 const in1 = document.getElementById("num1"); const in2 = document.getElementById("num2"); document.addEventListener("click", function(e) { if (e.target.classList.contains("operator")) { const optr = e.target.value const n1 =+ in1.value; const n2 =+ in2.value; let result = 'i dont know'; switch (optr) { case '+': result = n1 + n2 break; case '-': result = n1 - n2 break; case '*': result = n1 * n2; break; case '/': result = n1 / n2; } document.getElementById("output").innerHTML = "= " + result; } })
 Number 1:<input type="number" id="num1"><br><br> Number 2:<input type="number" id="num2"><br><br> <input type="button" value="+" class="operator"> <input type="button" value="-" class="operator"> <input type="button" value="*" class="operator"> <input type="button" value="/" class="operator"> <p id="output"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM