我无法在计算器的 num1 参数中添加两个字符
i can not add two characters in my num1 parameter in my calculator
我无法在 num1
中添加两个字符
我计算器中的参数请帮助我解决它。
如果有人能找到解决办法,请看一下。如果我为此设置任何条件,它需要 num2
中的 num1
值进行操作。例如,如果我选择 (89*2)
,它将执行 (89*89)
。事情就是这样....
<html>
<head>
<script>
var num1;
var num2;
var result;
var operations;
function getDisplay(digit)
{
if (display.value == "0" || display.value != "")
{
display.value = digit;
}
else
{
display.value = display.value + digit;
}
}
function clr()
{
display.value = "0";
//uprDisplay.value = "";
}
function addition()
{
operation = "+";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function subtraction(){
operation = "-";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function multiplication()
{
operation = "*";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function division()
{
operation = "/";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function equalto()
{
num2 = parseInt(display.value);
if (operation == "+")
{
result = num1 + num2;
}
else if (operation == "*")
{
result = num1 * num2;
}
else if (operation == "-")
{
result = num1 - num2;
}
else if (operation == "/")
{
result = num1 / num2;
}
//display.value = result;
display.value = num1 + operation + num2 + " = " + result;
}
</script>
</head>
<body>
<form name="calculator">
<table>
<!-- <tr>
<td colspan="4"><input type="text" name="uprDisplay" id="uprDisplay" style="text-align:right;" disabled="disabled" value=""/></td>
</tr> -->
<tr>
<td colspan="4"><input type="text" name="display" id="display" style="text-align:right;" disabled="disabled" value="0"/></td>
</tr>
<tr>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="seven" value="7"/></td>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="eight" value="8"/></td>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="nine" value="9"/></td>
<td><input type="button" id="button" onclick="division()" style="height: 30px; width: 100%;" name="div" value="/"/></td>
</tr>
<tr>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="four" value="4"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="five" value="5"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="six" value="6"/></td>
<td><input type="button" onclick="multiplication()" style="height: 30px; width: 100%;" name="mult" value="*"/></td>
</tr>
<tr>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="one" value="1"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="two" value="2"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="three" value="3"/></td>
<td><input type="button" onclick="subtraction()" style="height: 30px; width: 100%;" name="sub" value="-"/></td>
</tr>
<tr>
<td><input type="button" onclick="clr()" style="height: 30px; width: 100%;" name="clear" value="C"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="zero" value="0"/></td>
<td><input type="button" onclick="equalto()" style="height: 30px; width: 100%;" name="equal" value="="/></td>
<td><input type="button" onclick="addition()" style="height: 30px; width: 100%;" name="add" value="+"/></td>
</tr>
</table>
</form>
</body>
</html>
这个简单,改
if (display.value == "0" || display.value != "")
到
if (display.value === "0" || display.value === "")
编辑
好的,还有更多的错误。
所以,你看,在每次按下操作时,你都在将 display.value
更改为 String num1 + operation
,并且在 equalTo
方法中,你正在尝试 parseInt
this num1 + operation
String 这不会产生预期的结果。相反,使用 split(operation)
拆分 String,这将导致包含 2 个数字的 String 数组,并使用 parseInt()
在第二个数字(索引 1)上。
简而言之,在方法equalTo
中,
改变
num2 = parseInt(display.value);
至
num2 = parseInt(display.value.split(operation)[1]);
我无法在 num1
中添加两个字符
我计算器中的参数请帮助我解决它。
如果有人能找到解决办法,请看一下。如果我为此设置任何条件,它需要 num2
中的 num1
值进行操作。例如,如果我选择 (89*2)
,它将执行 (89*89)
。事情就是这样....
<html>
<head>
<script>
var num1;
var num2;
var result;
var operations;
function getDisplay(digit)
{
if (display.value == "0" || display.value != "")
{
display.value = digit;
}
else
{
display.value = display.value + digit;
}
}
function clr()
{
display.value = "0";
//uprDisplay.value = "";
}
function addition()
{
operation = "+";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function subtraction(){
operation = "-";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function multiplication()
{
operation = "*";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function division()
{
operation = "/";
num1 = parseInt(display.value);
display.value = num1 + operation;
//uprDisplay.value = num1 + operation;
}
function equalto()
{
num2 = parseInt(display.value);
if (operation == "+")
{
result = num1 + num2;
}
else if (operation == "*")
{
result = num1 * num2;
}
else if (operation == "-")
{
result = num1 - num2;
}
else if (operation == "/")
{
result = num1 / num2;
}
//display.value = result;
display.value = num1 + operation + num2 + " = " + result;
}
</script>
</head>
<body>
<form name="calculator">
<table>
<!-- <tr>
<td colspan="4"><input type="text" name="uprDisplay" id="uprDisplay" style="text-align:right;" disabled="disabled" value=""/></td>
</tr> -->
<tr>
<td colspan="4"><input type="text" name="display" id="display" style="text-align:right;" disabled="disabled" value="0"/></td>
</tr>
<tr>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="seven" value="7"/></td>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="eight" value="8"/></td>
<td><input type="button" id="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="nine" value="9"/></td>
<td><input type="button" id="button" onclick="division()" style="height: 30px; width: 100%;" name="div" value="/"/></td>
</tr>
<tr>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="four" value="4"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="five" value="5"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="six" value="6"/></td>
<td><input type="button" onclick="multiplication()" style="height: 30px; width: 100%;" name="mult" value="*"/></td>
</tr>
<tr>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="one" value="1"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="two" value="2"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="three" value="3"/></td>
<td><input type="button" onclick="subtraction()" style="height: 30px; width: 100%;" name="sub" value="-"/></td>
</tr>
<tr>
<td><input type="button" onclick="clr()" style="height: 30px; width: 100%;" name="clear" value="C"/></td>
<td><input type="button" onclick="getDisplay(this.value)" style="height: 30px; width: 100%;" name="zero" value="0"/></td>
<td><input type="button" onclick="equalto()" style="height: 30px; width: 100%;" name="equal" value="="/></td>
<td><input type="button" onclick="addition()" style="height: 30px; width: 100%;" name="add" value="+"/></td>
</tr>
</table>
</form>
</body>
</html>
这个简单,改
if (display.value == "0" || display.value != "")
到
if (display.value === "0" || display.value === "")
编辑
好的,还有更多的错误。
所以,你看,在每次按下操作时,你都在将 display.value
更改为 String num1 + operation
,并且在 equalTo
方法中,你正在尝试 parseInt
this num1 + operation
String 这不会产生预期的结果。相反,使用 split(operation)
拆分 String,这将导致包含 2 个数字的 String 数组,并使用 parseInt()
在第二个数字(索引 1)上。
简而言之,在方法equalTo
中,
改变
num2 = parseInt(display.value);
至
num2 = parseInt(display.value.split(operation)[1]);