首页 > 解决方案 > 我无法在计算器的 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>  

标签: javascriptcalculator

解决方案


这个很简单,改

if (display.value == "0" || display.value != "")

if (display.value === "0" || display.value === "")

编辑

好吧,还有更多的错误。因此,您会看到,在按下每个操作时,您正在更改display.valueString num1 + operation,并且在该方法equalTo中,您正在尝试使用不会产生所需结果的String 。相反,使用它拆分字符串将产生一个包含 2 个数字的字符串数组 ,并使用第二个数字(索引 1)。parseIntnum1 + operation split(operation)parseInt()

总之,在方法equalTo上,改变

num2 = parseInt(display.value);

num2 = parseInt(display.value.split(operation)[1]);


推荐阅读