Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript计算器有问题_Javascript - Fatal编程技术网

javascript计算器有问题

javascript计算器有问题,javascript,Javascript,正在尝试为项目创建javascript计算器。我不太擅长javascript,请帮助我。我没有错误,也没有显示任何内容。可能是事件监听器的问题。。?这是我的剧本: $(function () { "use strict"; var num1, num2, answer, operation = 0; var add = function (num1, num2) { var sum = num1 + num2; return sum;

正在尝试为项目创建javascript计算器。我不太擅长javascript,请帮助我。我没有错误,也没有显示任何内容。可能是事件监听器的问题。。?这是我的剧本:

$(function () {
    "use strict";
    var num1, num2, answer, operation = 0;

    var add = function (num1, num2) {
        var sum = num1 + num2;
        return sum;
    };
    var subtract = function (num1, num2) {
        var diff = num1 - num2;
        return diff;
    };
    var multiply = function (num1, num2) {
        var product = num1 * num2;
        return product;
    };
    var divide = function (num1, num2) {
        var quotient = num1 / num2;
        return quotient.toFixed(4);
    };

    $(".button").click(function () {
        var value = document.getElementById(this);
        switch (value) {
            case "zero":
                if (typeof(operation) == "string") {
                    num2 = 0;
                } else {
                    num1 = 0;
                }
                $("#display").append("0");
                break;
            case "one":
                if (typeof(operation) == "string") {
                    num2 = 1;
                } else {
                    num1 = 1;
                }
                $("#display").append("1");
                break;
            case "two":
                if (typeof(operation) == "string") {
                    num2 = 2;
                } else {
                    num1 = 2;
                }
                $("#display").append(2);
                break;
            case "three":
                if (typeof(operation) == "string") {
                    num2 = 3;
                } else {
                    num1 = 3;
                }
                $("#display").append(3);
                break;
            case "four":
                if (typeof(operation) == "string") {
                    num2 = 4;
                } else {
                    num1 = 4;
                }
                $("#display").append(4);
                break;
            case "five":
                if (typeof(operation) == "string") {
                    num2 = 5;
                } else {
                    num1 = 5;
                }
                $("#display").append(5);
                break;
            case "six":
                if (typeof(operation) == "string") {
                    num2 = 6;
                } else {
                    num1 = 6;
                }
                $("#display").append(6);
                break;
            case "seven":
                if (typeof(operation) == "string") {
                    num2 = 7;
                } else {
                    num1 = 7;
                }
                $("#display").append(7);
                break;
            case "eight":
                if (typeof(operation) == "string") {
                    num2 = 8;
                } else {
                    num1 = 8;
                }
                $("#display").append(8);
                break;
            case "nine":
                if (typeof(operation) == "string") {
                    num2 = 9;
                } else {
                    num1 = 9;
                }
                $("#display").append(9);
                break;
            case "add":
                operation = "+";
                break;
            case "subtract":
                operation = "-";
                break;
            case "multiply":
                operation = "*";
                break;
            case "divide":
                operation = "/";
                break;
            case "equals":
                if (operation == "+") {
                    answer = add(num1, num2);
                    $("#display").html(answer);
                } else if (operation == "-") {
                    answer = subtract(num1, num2);
                    $("#display").html(answer);
                } else if (operation == "*") {
                    answer = multiply(num1, num2);
                    $("#display").html(answer);
                } else if (operation == "/") {
                    answer = divide(num1, num2);
                    $("#display").html(answer);
                }
                else {
                    return;
                }
                break;
            case "clear":
                num1 = "";
                num2 = "";
                operation = 0;
                answer = "";
                $("#display").html(0);
                break;
        }
    });
});
HTML:


+
-
x
/
0
1.
2.
3.
4.
5.
6.
7.
8.
9
=
C

要获取所单击按钮的ID,您使用的是
document.getElementbyId()
,它在jQuery术语中类似于
$('#')
选择器,并且获取的是DOM元素,而不是按钮的ID。为此,请使用
$(this.attr('id')

没有错误的原因是它只是轻快地通过了
switch
语句,因为它基本上编写了很多
if
语句。它们都计算为false,代码继续

一个额外的建议,因为你是编程新手。一般来说,最好在
开关
的末尾包含一个
默认
大小写。如果所有其他值都为false,则将运行此代码

在代码末尾这样做有助于调试,以后可能会有用

case "clear":
       num1 = "";
       num2 = "";
       operation = 0;
       answer = "";
       $("#display").html(0);
       break;
default:
       console.log("ID not recognized!");
       break;
}

我知道做这个项目有很多简单的方法。我已经找到了更好的方法。然而,我是一个初学者,正在努力学习。你能加入你的HTML吗?我想也许你误解了getElementById应该做什么。请改为尝试value=this.id。有关为什么
default
s几乎总是必需的问题,请参见:。有时它就像
default:break一样简单哇!我知道事情会很简单。感谢您的及时回复和建议!
var value = $(this).attr('id');
switch (value) {...}
case "clear":
       num1 = "";
       num2 = "";
       operation = 0;
       answer = "";
       $("#display").html(0);
       break;
default:
       console.log("ID not recognized!");
       break;
}