[英]Can't figure out whats wrong with my program
嘿伙計們,當我運行這個程序時,它不會添加我的數字,基本上我應該打印銷售最暢銷的銷售員,以及最暢銷的汽車。 例如,當我在最后為每輛車輸入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);
提示的結果是一個字符串,所以+是連接的。 一個非常粗略的解決方法是做一些像:
temp = +prompt("Enter Mercedes-Benz amount: ");
其中+導致轉換。 請注意,這非常粗糙 - 它不會進行任何檢查或錯誤處理。
你需要將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;
}
工作小提琴
在將解析字符串添加為Integer之前。
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);
當您從用戶那里獲取輸入時,它將作為字符串接收。 您需要將此輸入轉換為int。 這可以通過使用parseInt(value, radix)
來完成。 要了解有關parseInt
函數的更多信息,請參閱http://www.w3schools.com/jsref/jsref_parseInt.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.