简单的 JavaScript 计算器

Muhammad Muzammil Hussain 2023年1月30日 2022年5月10日
  1. JavaScript 计算器
  2. 在 JavaScript 中使用 if-else if 语句构建计算器
  3. 在 JavaScript 中使用 switch 语句构建计算器
简单的 JavaScript 计算器

本文将让你了解如何在 JavaScript 上使用多种方式开发一个简单的计算器。要理解这一点,你必须了解以下 JavaScript 条件:

  • JavaScript If else if 语句
  • JavaScript switch 语句

JavaScript 计算器

我们在日常生活中使用计算器来执行许多数学运算,例如加法、减法、乘法。

我们还可以使用科学计算器来解决复杂问题,例如平方根和对数函数。

JavaScript 是构建用于添加网页的计算器的最常用和首选语言。

在 JavaScript 中使用 if-else if 语句构建计算器

<html>
    <head>
        <title>
            JavaScript calculator using If Else If
        </title>
    </head>
    <script>
        // getting number-1 from user
        var no1 = parseFloat(prompt("Please Enter your 1st No. : "));
        // getting operator to perform operation on two numbers
        var operator = prompt("Enter operator ( either +, -, * or / ) : ");
        // getting number-2 from user
        var no2 = parseFloat(prompt("Please Enter your 2nd No. : "));

        var result;

        // using if else if to perform operation according to the operator
        if (operator == "+") 
        {
		result = no1 + no2;  // to add two numbers
        }
        else if (operator == "-") 
        {
            result = no1 - no2;  // to subtract two numbers
        }
        else if (operator == "*") 
        {
            result = no1 * no2;  // to multiple two numbers
        }
        else
        {
            result = no1 / no2; // to divide two numbers
        }

        // Displaying the result
        window.alert("Required result is : " + result);

    </script>
    <body>
        <h2 style="text-align: center">Thats practical demo about JavaScript
                           calculator using JavaScript If Else If</h2>
    <body>
</html>

输出:

Please Enter your 1st No. : 12
Enter operator ( either +, -, * or / ) : +
Please Enter your 2nd No. : 13
Required result is : 25

That's a practical demo about JavaScript calculators using JavaScript. If Else If

在 JavaScript 中使用 switch 语句构建计算器

<html>
    <head>
        <title>
            JavaScript calculator using switch statement
        </title>
    </head>
    <script>
        // getting number-1 from user
        var no1 = parseFloat(prompt("Please Enter your 1st No. : "));
        // getting operator to perform operation on two numbers
        var operator = prompt("Enter operator ( either +, -, * or / ) : ");
        // getting number-2 from user
        var no2 = parseFloat(prompt("Please Enter your 2nd No. : "));

        var result;

        // using switch statement to operate according to the operator
        switch(operator) 
        {
            case "+":
                result = no1 + no2;   // adding two numbers
                break;

            case "-":
                result = no1 - no2;   // subtracting two numbers
                break;

            case "*":
                result = no1 * no2;   // multiplying two numbers
                break;

            case "/":
                result = no1 / no2;   // dividing two numbers
                break;

            default:
                console.log('Invalid operator');
                break;
        }

        // Displaying the result
        window.alert("Required result is : " + result);
    </script>  
    <body>  
        <h2 style="text-align: center">Thats practical demo about JavaScript
                    calculator using JavaScript switch statement</h2>
    <body>
</html>

输出:

Please Enter your 1st No. : 25
Enter operator ( either +, -, * or / ) : /
Please Enter your 2nd No. : 5
Required result is : 5

That's a practical demo about JavaScript calculator using JavaScript switch statement