<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>运算器</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
font-family:"宋体";
font-size:12px;
color:#333;
}
.fm1, .fm2 {
width:300px;
margin:30px auto;
background:#CCC;
padding:30px 0 30px 0;
}
.fm1 input, .fm2 input {
width:120px;
margin:0 auto;
display:block;
}
</style>
</head>
<body>
<form class="fm1">
<input type="button" value="两个数字的运算器1" οnclick="comp();" />
</form>
<script type="text/javascript">
//函数
function comp(){
while(true){
//接收输入
var num1=prompt("请输入第一个数字:","");
var num2=prompt("请输入第二个数字:","");
var cp=prompt("请输入符号:","");
//判断输入的符号,进行运算
if(cp=="*"){
document.write("计算结果:"+num1+"*"+num2+"="+(parseInt(num1)*parseInt(num2)));
break;
}
else if(cp=="/"){
document.write("计算结果:"+num1+"/"+num2+"="+(parseInt(num1)/parseInt(num2)));
break;
}
else if(cp=="+"){
document.write("计算结果:"+num1+"+"+num2+"="+(parseInt(num1)+parseInt(num2)));
break;
}
else if(cp=="-"){
document.write("计算结果:"+num1+"-"+num2+"="+(parseInt(num1)-parseInt(num2)));
break;
}
else{
document.write("输入有误,无法计算,请重新输入!");
var flag=confirm("是否要重新输入!");
if(flag==false){
document.write("已退出程序·");
break;
}
}
}
}
</script>
</body>
</html>
效果图:
个人E-mail:chaoyi77@163.com