使用jsp制作简单的计算器

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Mr_hanghang/article/details/102733026

简单的计算器

效果图:

在这里插入图片描述

以下是代码

HTML代码

	
		<input type="text" id="shu"  value=""/><br/>
		
		<input type="button" value="1" name="1"  onclick="calCulate(this.value)"/>
		<input type="button" value="2" name="2" onclick="calCulate(this.value)"/>
		<input type="button" value="3" name="3" onclick="calCulate(this.value)"/>
		<input type="button" value="+" name="+" onclick="calCulate(this.value)"/><br/>
		
		<input type="button" value="4" name="4" onclick="calCulate(this.value)"/>
		<input type="button" value="5" name="5" onclick="calCulate(this.value)"/>
		<input type="button" value="6" name="6" onclick="calCulate(this.value)"/>
		<input type="button" value="-" name="-" onclick="calCulate(this.value)"/><br/>
		
		<input type="button" value="7" name="7" onclick="calCulate(this.value)"/>
		<input type="button" value="8" name="8" onclick="calCulate(this.value)"/>
		<input type="button" value="9" name="9" onclick="calCulate(this.value)"/>
		<input type="button" value="*" name="*" onclick="calCulate(this.value)"/><br/>
		
		<input type="button" value="A" name="A" onclick="calCulate(this.value)"/>
		<input type="button" value="0" name="0" onclick="calCulate(this.value)"/>
		<input type="button" value="=" name="=" onclick="calCulate(this.value)"/>
		<input type="button" value="/" name="/" onclick="calCulate(this.value)"/><br/>
		

JSP代码

<script type="text/javascript">
			
			function calCulate(val){
				
				var num=document.getElementById("shu");
				switch(val){
					case "=":
					num.value=eval(num.value);
					break;
					case "A":
					num.value="";
					break;
					
					default:
					num.value=num.value+val;
					break;
				}
			}
		</script>

简单的计算器

本人初学者,代码如有BUG,请联系我QQ2455441814,或者评论区留言回复,谢谢并欢迎各位大佬指导

猜你喜欢

转载自blog.csdn.net/Mr_hanghang/article/details/102733026