- DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>编写一个带两个变量和一个运算符的四则运算函数title>
- head>
- <body>
- <input name="btn" type="button" value="计算两数运算结果" onclick="aaa()"/>
- body>
-
- html>
- <script>
- //四则运算
- function aaa() {
- var p1 = prompt ("请输入第一个数:","") ;
- var p2 = prompt ("请输入第二个数:","") ;
- var p3 = prompt ("请输入运算符号:","") ;
- var result ;
- p1 = parseInt(p1) ;
- p2 = parseInt(p2) ;
- switch (p3){
- case "+":
- result = p1+p2 ;
- break;
-
- case "-":
- result = p1-p2 ;
- break;
-
- case "*":
- result = p1*p2 ;
- break;
-
- case "/":
- result = p1/p2 ;
- break;
-
- default:
- alert ("你输入有误!!!") ;
- break;
- }
- alert ("两数运算结果为:"+p1+p3+p2+"="+result) ;
- }
- script>
- body>