Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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,嘿,伙计们,当我运行这个程序时,它不会添加我的数字,基本上,我应该打印出销售最高的销售人员和最畅销的汽车。例如,当我在末尾为每辆车键入50 amount时,它会打印:0505050,而不是将其相加为200 function salesPerson(name, id, amount) { this.name = name; this.id = id; this.amount = amount; };

嘿,伙计们,当我运行这个程序时,它不会添加我的数字,基本上,我应该打印出销售最高的销售人员和最畅销的汽车。例如,当我在末尾为每辆车键入50 amount时,它会打印:0505050,而不是将其相加为200

function salesPerson(name, id, amount) {
            this.name = name;
            this.id = id;
            this.amount = amount;
        };


    //Create car array for each brand
        var Car = new Array(4);

        //Create array of salePersons
        var Person = new Array(2);

        //Every element in the array is a object type salesPerson
        for (var i = 0; i < Person.length; i++) {
            Person[i] = new salesPerson("", 0, 0);
        }

        var temp = 0;

        var m = 0;
        var a = 0;
        var p = 0;
        var b = 0;



        for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

        var max = 0;
        var name = "";

        for (var i = 0; i < Person.length; i++) {
            if (Person[i].amount > max) {
                max = Person[i].amount;
                name = Person[i].name;
            }
        }

        alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);

提示符的结果是一个字符串,因此+是串联的。一个非常粗糙的解决方法是只执行以下操作:

function salesPerson(name, id, amount) {
            this.name = name;
            this.id = id;
            this.amount = amount;
        };


    //Create car array for each brand
        var Car = new Array(4);

        //Create array of salePersons
        var Person = new Array(2);

        //Every element in the array is a object type salesPerson
        for (var i = 0; i < Person.length; i++) {
            Person[i] = new salesPerson("", 0, 0);
        }

        var temp = 0;

        var m = 0;
        var a = 0;
        var p = 0;
        var b = 0;



        for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

        var max = 0;
        var name = "";

        for (var i = 0; i < Person.length; i++) {
            if (Person[i].amount > max) {
                max = Person[i].amount;
                name = Person[i].name;
            }
        }

        alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);
temp = +prompt("Enter Mercedes-Benz amount: ");
其中+导致转换。请注意,这是非常粗糙的-它不做任何检查或错误处理。

您需要将temp转换为number;使用一元运算符+或parseInt

function salesPerson(name, id, amount) {
            this.name = name;
            this.id = id;
            this.amount = amount;
        };


    //Create car array for each brand
        var Car = new Array(4);

        //Create array of salePersons
        var Person = new Array(2);

        //Every element in the array is a object type salesPerson
        for (var i = 0; i < Person.length; i++) {
            Person[i] = new salesPerson("", 0, 0);
        }

        var temp = 0;

        var m = 0;
        var a = 0;
        var p = 0;
        var b = 0;



        for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

        var max = 0;
        var name = "";

        for (var i = 0; i < Person.length; i++) {
            if (Person[i].amount > max) {
                max = Person[i].amount;
                name = Person[i].name;
            }
        }

        alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);
 for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = +prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = +prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = +prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = +prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

在将解析字符串添加为整数之前工作

function salesPerson(name, id, amount) {
            this.name = name;
            this.id = id;
            this.amount = amount;
        };


    //Create car array for each brand
        var Car = new Array(4);

        //Create array of salePersons
        var Person = new Array(2);

        //Every element in the array is a object type salesPerson
        for (var i = 0; i < Person.length; i++) {
            Person[i] = new salesPerson("", 0, 0);
        }

        var temp = 0;

        var m = 0;
        var a = 0;
        var p = 0;
        var b = 0;



        for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

        var max = 0;
        var name = "";

        for (var i = 0; i < Person.length; i++) {
            if (Person[i].amount > max) {
                max = Person[i].amount;
                name = Person[i].name;
            }
        }

        alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);
职能销售人员姓名、id、金额{ this.name=名称; this.id=id; 这个。金额=金额; }; //为每个品牌创建汽车阵列 var Car=新阵列4; //创建销售人员数组 var Person=新阵列2; //数组中的每个元素都是对象类型 对于var i=0;i最大值{ max=人[i]。金额; 姓名=人[i]。姓名; } }
Alert本月最佳销售人员为+name+,销售金额为$+max 当您从用户获取输入时,它将作为字符串接收。您需要将此输入转换为整数。这可以通过使用parseIntvalue、基数来完成。要了解关于parseInt函数的更多信息,请参见javascript中的

,0+50=050和0+parseInt50=50,我猜它是作为字符串出现的。因此,当您添加“0”+“50”+“50”+“50”+“50”时,您会得到“0505050”,为什么不在结果上使用parseInt呢?他将其存储为tempparseInt,只要使用+就可以了。ParseInt具有检测无效字符的优势我有一个名为function->isNegativenum{if num<0{alertTry,数量为正数!;返回false;}返回true;};我用它来验证上面的金额,但是如果我输入两位数字,程序就结束了
function salesPerson(name, id, amount) {
            this.name = name;
            this.id = id;
            this.amount = amount;
        };


    //Create car array for each brand
        var Car = new Array(4);

        //Create array of salePersons
        var Person = new Array(2);

        //Every element in the array is a object type salesPerson
        for (var i = 0; i < Person.length; i++) {
            Person[i] = new salesPerson("", 0, 0);
        }

        var temp = 0;

        var m = 0;
        var a = 0;
        var p = 0;
        var b = 0;



        for(var i = 0; i < Person.length; i++){
           Person[i].name = prompt("Enter salesman name: ");
           Person[i].id = prompt("Enter salesman id: ");

           temp = prompt("Enter Mercedes-Benz amount: ");
           Person[i].amount += temp;
           m += temp;

           temp = prompt("Enter Audi amount: ");
           Person[i].amount += temp;
           a += temp;

           temp = prompt("Enter Porsche amount: ");
           Person[i].amount += temp;
           p += temp;

           temp = prompt("Enter BMW amount: ");
           Person[i].amount += temp;
           b += temp;

        }

        var max = 0;
        var name = "";

        for (var i = 0; i < Person.length; i++) {
            if (Person[i].amount > max) {
                max = Person[i].amount;
                name = Person[i].name;
            }
        }

        alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);