我是初学者,而且我已经被困了好几个小时。我有一个简单的for循环问题。我想要一个警告框,它给出了玩家控制的条形图的总和(它是简单桌面游戏代码的一部分)
这是我的代码:
var districts = [{bars:0}, {bars:2}, {bars:2}, {bars:3}, {bars:0}, {bars:3}]; //these are all the districts in the game + the amount of bars on them
var PlayerControlledDistricts = [districts[0], districts[2], districts[3]]; //these are the 3 districts the player controlls
var AmountOfBars = 0;
for (i = 0; i <=PlayerControlledDistricts.length; i++){
AmountOfBars += parseInt(PlayerControlledDistricts[i].bars, 10);
}
alert(AmountOfBars);
这段代码有什么问题?如果我把一个警告框放在for循环中我得到:0,2,5 - 所以我认为循环内部的一切都很好 - 但是一旦我把警报放在循环外面就没有任何事情发生......
答案 0 :(得分:5)
问题是你循环了太多元素,使用<
代替<=
var districts = [{bars:0}, {bars:2}, {bars:2}, {bars:3}, {bars:0}, {bars:3}]; //these are all the districts in the game + the amount of bars on them
var PlayerControlledDistricts = [districts[0], districts[2], districts[3]]; //these are the 3 districts the player controlls
var AmountOfBars = 0;
for (i = 0; i < PlayerControlledDistricts.length; i++){
AmountOfBars += parseInt(PlayerControlledDistricts[i].bars, 10);
}
alert(AmountOfBars);
正如@Jamiec指出的那样,你实际上并不需要解析数字,因为你已经把它作为一个数字,所以就这样做:
AmountOfBars += PlayerControlledDistricts[i].bars;
答案 1 :(得分:0)
您需要使用<
代替<=
!
var districts = [{bars:0}, {bars:2}, {bars:2}, {bars:3}, {bars:0}, {bars:3}]; //these are all the districts in the game + the amount of bars on them
var PlayerControlledDistricts = [districts[0], districts[2], districts[3]]; //these are the 3 districts the player controlls
var AmountOfBars = 0;
for (i = 0; i < PlayerControlledDistricts.length; i++) {
debugger;
AmountOfBars += parseInt(PlayerControlledDistricts[i].bars, 10);
}
alert(AmountOfBars);
&#13;
答案 2 :(得分:0)
将您的更改为:
for (i = 0; i < PlayerControlledDistricts.length; i++)`
这是因为一个数组从0开始,其长度从1开始,你需要计算最多PlayerControlledDistricts.length
,因为PlayerControlledDistricts.length
本身比数组本身的索引高1。 / p>
答案 3 :(得分:0)
问题在于你的迭代 - 你正在迭代到i <=PlayerControlledDistricts.length
,这比你需要的多一个,所以最后在最后一个未定义的循环上要求PlayerControlledDistricts[3]
。您也不需要parseInt
,因为您已经有了数值。这是一个使用console.log的工作示例:
var districts = [{bars:0}, {bars:2}, {bars:2}, {bars:3}, {bars:0}, {bars:3}]; //these are all the districts in the game + the amount of bars on them
var PlayerControlledDistricts = [districts[0], districts[2], districts[3]]; //these are the 3 districts the player controlls
var AmountOfBars = 0;
for (i = 0; i < PlayerControlledDistricts.length; i++) {
AmountOfBars += PlayerControlledDistricts[i].bars;
}
console.log(AmountOfBars);
JSFiddle:http://jsfiddle.net/eofbenw2/
答案 4 :(得分:0)
数组的索引是问题,因为请记住数组的元素应该是0 .....到n-1,因为length属性有n个元素。 所以如果你有一个包含10个元素的数组,那么长度将是10,并且元素存储在0到9之间。
之后,这是一个很好的做法,用for for for var for(var i = 0; condition; increment);
另外,您不需要转换。
我建议您使用Console.log方法验证信息和对象,在Web浏览器的控制台中打印控制台(您可以在控制台选项卡中使用F12进行观看)。
var districts = [{bars:0}, {bars:2}, {bars:2}, {bars:3}, {bars:0}, {bars:3}]; //these are all the districts in the game + the amount of bars on them
var PlayerControlledDistricts = [districts[0], districts[2], districts[3]]; //these are the 3 districts the player controlls
var AmountOfBars = 0;
for (var i = 0; i <PlayerControlledDistricts.length; i++)
AmountOfBars += PlayerControlledDistricts[i].bars;
alert(AmountOfBars);
此致