无法弄清楚我的程序有什么问题

时间:2015-02-22 04:49:42

标签: javascript

嘿伙计们,当我运行这个程序时,它并没有添加我的数字,基本上我应该打印最畅销的推销员,以及最畅销的汽车。例如,当我在最后为每辆车输入50金额时,它会打印:050505050,而不是将它总计为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);

4 个答案:

答案 0 :(得分:0)

提示的结果是一个字符串,所以+是连接的。一个非常粗略的解决方法是做一些像:

temp = +prompt("Enter Mercedes-Benz amount: ");

其中+导致转换。请注意,这非常粗糙 - 它不会进行任何检查或错误处理。

答案 1 :(得分:0)

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

 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;

        }

工作Fiddle

答案 2 :(得分:0)

在将解析字符串添加为Integer之前

&#13;
&#13;
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 = parseInt(prompt("Enter Mercedes-Benz amount: "));
           Person[i].amount += temp;
           m += temp;

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

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

           temp = parseInt(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);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

当您从用户那里获取输入时,它将作为字符串接收。您需要将此输入转换为int。这可以通过使用parseInt(value, radix)来完成。要详细了解parseInt功能,请参阅http://www.w3schools.com/jsref/jsref_parseInt.asp