欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js实现简单的四则运算

程序员文章站 2022-03-08 23:40:16
...

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>四则运算</title>
		<script type="text/javascript">
			function account(){
				var num1 = prompt("请输入第一个值:","");
				// alert(typeof(num1));
				var num11 = parseInt(num1);
				
				var num2 = prompt("请输入第二个值:","");
				var num22 = parseInt(num2);
				
				var ysf = prompt("请输入运算符:","");
				var result ;
				switch(ysf){
					case "+":
						result = num11 + num22;
						break;
					case "-":
						result = num11 - num22;
						break;
					case "*":
						result = num11 * num22;
						break;
					case "/":
						if(num22!=0){
							result = num11 / num22;
						}else{
							alert("除数不能为0");
						}
						break;
				}
				document.write(result+"<br />");
				
			}
			
			
		</script>
	</head>
	<body>
		<button οnclick="account()">点击计算两个数的值</button>	
	</body>
</html>

js实现简单的四则运算

 

相关标签: JavaScript学习