JavaScript_03 超简计算器

版本一:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
    <script type="text/javascript">
        function add(){
            let num1 = document.getElementById("number1").value;
            let num2 = document.getElementById("number2").value;
            if(num1 == ""){
                alert("数字1不能为空");
                return;
            }
            if (isNaN(num1)){
                alert("数字1输入错误");
                return;
            }
            if(num2 == ""){
                alert("数字2不能为空");
                return;
            }
            if (isNaN(num2)){
                alert("数字2输入错误");
                return;
            }
            let num3 = parseInt(num1) + parseInt(num2);
            document.getElementById("result").value = num3;
        }
        function minus(){

            let num1 = document.getElementById("number1").value;
            let num2 = document.getElementById("number2").value;
            if(num1 == ""){
                alert("数字1不能为空");
                return;
            }
            if (isNaN(num1)){
                alert("数字1输入错误");
                return;
            }
            if(num2 == ""){
                alert("数字2不能为空");
                return;
            }
            if (isNaN(num2)){
                alert("数字2输入错误");
                return;
            }
            let num3 = parseInt(num1) - parseInt(num2);
            document.getElementById("result").value = num3;
        }
        function multiplication(){
            let num1 = document.getElementById("number1").value;
            let num2 = document.getElementById("number2").value;
            if(num1 == ""){
                alert("数字1不能为空");
                return;
            }
            if (isNaN(num1)){
                alert("数字1输入错误");
                return;
            }
            if(num2 == ""){
                alert("数字2不能为空");
                return;
            }
            if (isNaN(num2)){
                alert("数字2输入错误");
                return;
            }
            let num3 = parseInt(num1) * parseInt(num2);
            document.getElementById("result").value = num3;
        }
        function divide(){
            let num1 = document.getElementById("number1").value;
            let num2 = document.getElementById("number2").value;
            if(num1 == ""){
                alert("数字1不能为空");
                return;
            }
            if (isNaN(num1)){
                alert("数字1输入错误");
                return;
            }
            if(num2 == ""){
                alert("数字2不能为空");
                return;
            }
            if (isNaN(num2)){
                alert("数字2输入错误");
                return;
            }

            if (num2 == 0){
            alert("被除数不能为0")
            return;
          }
            let num3 = parseInt(num1) / parseInt(num2);
            document.getElementById("result").value = num3;

        }
        function remainder(){
            let num1 = document.getElementById("number1").value;
            let num2 = document.getElementById("number2").value;
            if(num1 == ""){
                alert("数字1不能为空");
                return;
            }
            if (isNaN(num1)){
                alert("数字1输入错误");
                return;
            }
            if(num2 == ""){
                alert("数字2不能为空");
                return;
            }
            if (isNaN(num2)){
                alert("数字2输入错误");
                return;
            }
            let num3 = parseInt(num1) % parseInt(num2);
            document.getElementById("result").value = num3;
        }
    </script>
</head>
<body>
数字1:<input type="text" id="number1"><br>
数字2:<input type="text" id="number2"><br>
<input type="button" value="加" id="add" onclick="add()">
<input type="button" value="减" id="minus" onclick="minus()">
<input type="button" value="乘" id="multiplication" onclick="multiplication()">
<input type="button" value="除" id="divide" onclick="divide()">
<input type="button" value="取余" id="remainder" onclick="remainder()"><br>
结果:<input type="text" id = "result">


</body>
</html>

结果演示:

版本二:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>计算器</title>
  <script type="text/javascript">
    function checkData(){
      let num1 = document.getElementById("number1").value;
      let num2 = document.getElementById("number2").value;
      if(num1 == ""){
        alert("数字1不能为空");
        return false;
      }
      if (isNaN(num1)){
        alert("数字1输入错误");
        return false;
      }
      if(num2 == ""){
        alert("数字2不能为空");
        return false;
      }
      if (isNaN(num2)){
        alert("数字2输入错误");
        return false;
      }
      return true;
    }

    function run(choose){
      if (!checkData()){return;}
      let num1 = parseInt(document.getElementById("number1").value);
      let num2 = parseInt(document.getElementById("number2").value);
      let num3;
      switch (choose){
        case '加':
          num3 = num1 + num2;
          break;
        case '减':
          num3 = num1 - num2;
          break;
        case '乘':
          num3 = num1 * num2;
          break;
        case '除':
            if (num2 == 0){
            alert("被除数不能为0")
            return;
          }
          num3 = num1 / num2;
          break;
        case '取余':
          num3 = num1 % num2;
          break;
      }
      document.getElementById("result").value = num3;
    }
  </script>
</head>
<body>
数字1:<input type="text" id="number1"><br>
数字2:<input type="text" id="number2"><br>
<input type="button" value="加" id="add" onclick="run('加')">
<input type="button" value="减" id="minus" onclick="run('减')">
<input type="button" value="乘" id="multiplication" onclick="run('乘')">
<input type="button" value="除" id="divide" onclick="run('除')">
<input type="button" value="取余" id="remainder" onclick="run('取余')"><br>
结果:<input type="text" id = "result">
</body>
</html>

效果同上

相关推荐
牧羊狼的狼7 分钟前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手2 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one2 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
魔云连洲2 小时前
深入解析:Vue与React的异步批处理更新机制
前端·vue.js·react.js
mCell2 小时前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
weixin_437830944 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
超级无敌攻城狮4 小时前
3 分钟学会!波浪文字动画超详细教程,从 0 到 1 实现「思考中 / 加载中」高级效果
前端
excel4 小时前
用 TensorFlow.js Node 实现猫图像识别(教学版逐步分解)
前端
gnip5 小时前
JavaScript事件流
前端·javascript
小菜全5 小时前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json