我无法在num1
中添加两个字符
我的计算器中的参数plz帮助我解决它。
如果有人能找到解决方案,请查看它。如果我为此设置任何条件,则num1
中的num2
值需要进行操作。例如,如果我将(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>
答案 0 :(得分:2)
这很简单,改变
if (display.value == "0" || display.value != "")
到
if (display.value === "0" || display.value === "")
修改强>
好的,还有更多错误。
因此,您看到,在每次操作时,您正在将display.value
更改为字符串 num1 + operation
,并且在equalTo
方法中,您正在尝试parseInt
此num1 + operation
字符串 split(operation)
,不会产生预期的结果。相反,使用parseInt()
拆分字符串,这将导致包含2个数字的字符串数组,并在第二个数字上使用equalTo
(指数1)。
简而言之,在方法num2 = parseInt(display.value);
中,
改变
num2 = parseInt(display.value.split(operation)[1]);
到
{{1}}