简体   繁体   English

计算器的CE按钮无法正常工作

[英]CE button of calculator is not working properly

CE button of my calculator is not working,it just clears all numbers on clicking CE button.I just want that on clicking CE button,it only delete last number and not all numbers and one more thing when the calculator load it will show default 0 value and when any number entered 0 will replace with that number 我的计算器的“ CE”按钮不起作用,单击“ CE”按钮时只清除所有数字。我只希望单击“ CE”按钮时,它只删除最后一个数字,而不是所有数字,并且在加载计算器时会再显示一则默认值0值,当输入任何数字0时将替换为该数字

 <html> <head> <title>Calculator </title> <link rel="stylesheet" href="index.css"> </head> <body> <center> <form name="calculator"> <div style=" width: 200px;height: 250px;border: 1px solid #D0CECE;"> <table style="margin-top:20px;"> <tr> <td> <input name="displayresult" id="display" class="cal-input" disabled > </td> </tr> </table> <table border="0px" cellspacing="10px" style="margin-top:5px;"> <tr> <td name="left" value="(" onclick="calculator.display.value += '('" class="cal-top">(</td> <td name="right" value=")" onclick="calculator.display.value += ')'" class="cal-top"> )</td> <td class="operator cal-top" name="percent" value="%" onclick="calculator.display.value += '%'">%</td> <td id="clear" name="clear" value="c" onclick="calculator.display.value = ''" class=" cal-top">CE</td> </tr> <tr> <td name="seven" value="7" onclick="calculator.display.value += '7'" class="cal-number">7</td> <td name="eight" value="8" onclick="calculator.display.value += '8'" class="cal-number">8</td> <td name="nine" value="9" onclick="calculator.display.value += '9'" class="cal-number">9</td> <td class="operator cal-top" name="div" value="/" onclick="calculator.display.value += '/'">/</td> </tr> <tr> <td name="four" value="4" onclick="calculator.display.value += '4'" class="cal-number">4</td> <td name="five" value="5" onclick="calculator.display.value += '5'" class="cal-number">5</td> <td name="six" value="6" onclick="calculator.display.value += '6'" class="cal-number">6</td> <td class="operator cal-top" name="times" value="*" onclick="calculator.display.value += '*'">*</td> </tr> <tr> <td name="one" value="1" onclick="calculator.display.value += '1'" class="cal-number">1</td> <td name="two" value="2" onclick="calculator.display.value += '2'" class="cal-number">2</td> <td name="three" value="3" onclick="calculator.display.value += '3'" class="cal-number">3</td> <td class="operator cal-top" name="minus" value="-" onclick="calculator.display.value += '-'">-</td> </tr> <tr> <td name="zero" value="0" onclick="calculator.display.value += '0'" class="cal-number">0</td> <td name="decimal" value="." onclick="calculator.display.value += '.'" class="cal-number">.</td> <td name="result" value="=" onclick="calculator.display.value = eval(calculator.display.value)" class="cal-result"><b>=</b></td> <td class="operator cal-top" name="plus" value="+" onclick="calculator.display.value += '+'">+</td> </tr> </table> </div> </form> </center> </body> </head> </html> 

Change the CE button onclick function to the following: CE按钮的onclick功能更改为以下内容:

onclick="str = calculator.display.value.slice(0, -1);calculator.display.value = str;"

Which gets the value of the calculator output, and removes the last character from it and puts that in a variable named str , and then sets the calculator output to the value of str . 它获取计算器输出的值,并从中删除最后一个字符,并提出,在一个变量命名的str ,然后设置计算器输出的值str

Then add placeholder="0" to the <input> which displays the calculator output: 然后将placeholder="0"添加到显示计算器输出的<input>上:

<input name="displayresult" id="display" class="cal-input" placeholder="0" disabled>

Which makes the <input> display the value 0 when it contains nothing, and it goes away when characters are added to the box. 这使得<input>在不包含任何内容时显示值为0 ,而在将字符添加到框中时消失。

 <html> <head> <title>Calculator </title> <link rel="stylesheet" href="index.css"> </head> <body> <center> <form name="calculator"> <div style=" width: 200px;height: 250px;border: 1px solid #D0CECE;"> <table style="margin-top:20px;"> <tr> <td> <input name="displayresult" id="display" class="cal-input" placeholder="0" disabled> </td> </tr> </table> <table border="0px" cellspacing="10px" style="margin-top:5px;"> <tr> <td name="left" value="(" onclick="calculator.display.value += '('" class="cal-top">(</td> <td name="right" value=")" onclick="calculator.display.value += ')'" class="cal-top"> )</td> <td class="operator cal-top" name="percent" value="%" onclick="calculator.display.value += '%'">%</td> <td id="clear" name="clear" value="c" onclick="str = calculator.display.value.slice(0, -1); calculator.display.value = str" class=" cal-top">CE</td> </tr> <tr> <td name="seven" value="7" onclick="calculator.display.value += '7'" class="cal-number">7</td> <td name="eight" value="8" onclick="calculator.display.value += '8'" class="cal-number">8</td> <td name="nine" value="9" onclick="calculator.display.value += '9'" class="cal-number">9</td> <td class="operator cal-top" name="div" value="/" onclick="calculator.display.value += '/'">/</td> </tr> <tr> <td name="four" value="4" onclick="calculator.display.value += '4'" class="cal-number">4</td> <td name="five" value="5" onclick="calculator.display.value += '5'" class="cal-number">5</td> <td name="six" value="6" onclick="calculator.display.value += '6'" class="cal-number">6</td> <td class="operator cal-top" name="times" value="*" onclick="calculator.display.value += '*'">*</td> </tr> <tr> <td name="one" value="1" onclick="calculator.display.value += '1'" class="cal-number">1</td> <td name="two" value="2" onclick="calculator.display.value += '2'" class="cal-number">2</td> <td name="three" value="3" onclick="calculator.display.value += '3'" class="cal-number">3</td> <td class="operator cal-top" name="minus" value="-" onclick="calculator.display.value += '-'">-</td> </tr> <tr> <td name="zero" value="0" onclick="calculator.display.value += '0'" class="cal-number">0</td> <td name="decimal" value="." onclick="calculator.display.value += '.'" class="cal-number">.</td> <td name="result" value="=" onclick="calculator.display.value = eval(calculator.display.value)" class="cal-result"><b>=</b></td> <td class="operator cal-top" name="plus" value="+" onclick="calculator.display.value += '+'">+</td> </tr> </table> </div> </form> </center> </body> </head> </html> 

You can apply simple maths for this.Division and multiplication of 10 For CE : Take the number divide it by 10 and return the integer. 您可以对此应用简单的数学运算。10的除法和乘法对于CE :用数字除以10并返回整数。 Ex: you pressed 103 already and now you click on CE. 例如:您已经按了103,现在单击CE。 now 103/10 = 10(in integer). 现在103/10 = 10(整数)。 For 0 Replacement :the same reverse logic you can apply for the replacement of default zero by a number pressed Ex:It is displaying 0 initially now you press 2 0(old value)*10+2(new key pressed)= 2 after you press 6 2(old value)*10+6(new key pressed)= 26 对于0替换 :可以使用相同的反向逻辑来申请按所按的数字替换默认零例如:最初显示0现在现在按2 0(旧值)* 10 + 2(按下新键)= 2按下6 2(旧值)* 10 + 6(按下新键)= 26

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

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