我不能在我的num1
中添加两个字符 我的计算器中的参数请帮助我解决它。
如果有人能找到解决方案,请查看它。如果我为此提出任何条件,则需要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>
这很简单,改变
if (display.value == "0" || display.value != "")
自
if (display.value === "0" || display.value === "")
编辑
好的,还有更多的错误。 因此,您会看到,在每个操作按下时,您正在将display.value
更改为字符串num1 + operation
,并且在equalTo
方法中,您正在尝试parseInt
这个不会产生所需结果的num1 + operation
字符串。相反,使用split(operation)
拆分字符串,这将导致包含 2 个数字的字符串数组,并在第二个数字(索引 1(上使用parseInt()
。
总之,在方法equalTo
, 改变
num2 = parseInt(display.value);
自
num2 = parseInt(display.value.split(operation)[1]);