今日小案例,用Js实现一个小小的计算器
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>简易计算器</title> </head> <script> function calculate(num1, num2) { return [num1 + num2, num1 - num2, num1 * num2, num1 / num2]; } while (true) { var choose = prompt('欢迎使用简易计算器\n 1.加法运算\n 2.减法运算\n 3.乘法运算\n 4.除法运算\n 5.退出'); if (choose == '5') { alert('正在退出...'); break; } else { var num1 = prompt('请输入第一个数'); var num2 = prompt('请输入第二个数'); var re = calculate(parseInt(num1), parseInt(num2)); switch (choose) { case '1': alert('加法的输出结果是:' + re[0]); break; case '2': alert('减法的输出结果是:' + re[1]); break; case '3': alert('乘法的输出结果是:' + re[2]); break; case '4': alert('除法的输出结果是:' + re[3]); break; default: alert('输入错误!'); break; } } } </script> <body> </body> </html>
标签:num1,num2,alert,break,re,计算器,Js,加减乘除 From: https://www.cnblogs.com/Xiaojianlogs/p/16704825.html