javascript - 我无法在计算器的 num1 参数中添加两个字符
问题描述
我不能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
为String num1 + operation
,并且在该方法equalTo
中,您正在尝试使用不会产生所需结果的String 。相反,使用它拆分字符串将产生一个包含 2 个数字的字符串数组 ,并使用第二个数字(索引 1)。parseInt
num1 + operation
split(operation)
parseInt()
总之,在方法equalTo
上,改变
num2 = parseInt(display.value);
至
num2 = parseInt(display.value.split(operation)[1]);
推荐阅读
- screen-resolution - 网站元素被隐藏在 125% 推荐的笔记本电脑缩放
- ios - 在文本文件中复制调查并粘贴到我的应用程序中
- scala - Kafka Streams:了解 groupByKey 和 windowedBy
- python - 如何阻止我的图表的标签出现在图表内?
- c# - DI 和基类
- javascript - 使用 getter 在 React 组件中渲染另一个组件是一种好习惯吗?
- php - 从 Docker 容器使用 localhost 连接到 MariaDB
- javafx - 是否可以在 ZuluFX for Mac 上使用 javapackager
- python-2.7 - flask_wtf SelectField(错误:选择无效)
- php - 我的php中的数组到字符串转换问题