标签:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>计算器</title> 6 <script> 7 function cal(num1,num2,sign){ 8 num1=Number(num1); 9 num2=Number(num2); 10 var s; 11 switch(sign){ 12 case "+": 13 s=num1+num2; 14 break; 15 case "-": 16 s=num1-num2; 17 break; 18 case "*": 19 s=num1*num2; 20 break; 21 case "/": 22 if(num2){ 23 s=num1/num2; 24 }else{s="error";} 25 break; 26 case "%": 27 if(num2){ 28 s=num1%num2; 29 }else{s="error"} 30 break; 31 } 32 return (s); 33 } 34 var n1,n2,sig; 35 do{ 36 n1=prompt("请输入第一个数"); 37 var flag=(!isNaN(n1))?0:1; 38 }while(flag) 39 do{ 40 n2=prompt("请输入第一个数(除数和被取余数不能为0!)"); 41 var flag=(!isNaN(n2))?0:1; 42 }while(flag) 43 do{ 44 sig=prompt("请输入第一个数"); 45 var flag=(sig=="+" || sig=="-" || sig=="*" || sig=="/" || sig=="%")?0:1; 46 }while(flag) 47 document.write(n1+sig+n2+"="+cal(n1,n2,sig)); 48 </script> 49 </head> 50 <body> 51 52 </body> 53 </html>
标签:
原文地址:http://www.cnblogs.com/xiaomume/p/4691450.html