我正在测试一个岩纸剪刀蜥蜴spock游戏。我使用matrix[3,0]
来捕捉所有可能性,而不是产生可能的结果。在数组位置console.log(matrix[3,0])
,输入的字符串是“摇滚胜利”。但是,我在数组后面运行var matrix = [];
matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
log.console(matrix[3,0]); //returns spock wins
并产生了不同的结果。
有人可以告诉我为什么会这样吗?
{{1}}
答案 0 :(得分:4)
不,它没有。它实际上回归“摇滚胜利” 简而言之,您使用的语法不正确。
您可能会错过这一部分并直接进入“它应该如何?”如果你对理解JavaScript的神奇行为不感兴趣。
matrix[0,0]
时,0,0
被识别为作为表达式,其被计算为最后一个数字(在本例中为0)。 这意味着
matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
// ...
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
console.log(matrix[3,0]);
与
相同matrix[0] = "it's a tie";
matrix[1] = "Paper wins";
matrix[2] = "rock wins";
// ...
matrix[3] = "Scissors win";
matrix[4] = "spock wins";
matrix[0] = "rock wins";
console.log(matrix[0]); // returns "rock wins"
详细了解in this StackOverflow topic。
原因是你使用了错误的语法。
这就是你在JavaScript中访问多维数组中的值的方法:
var val = arr[key1][key2][key3]; // not arr[key1, key2, key3]
这就是你的代码应该是这样的:
var matrix = [];
for (var i = 0; i < 4; i++) matrix[i] = [];
matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
// ...
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";
document.write(matrix[3][0]); // not matrix[3,0]
另一个好的选择是以这种方式描述您的2D阵列:
var matrix = [
["it's a tie", "Paper wins", "rock wins"],
["Paper wins", "it's a tie", "Scissors wins"]
// ...
];
答案 1 :(得分:1)
在您的代码中:
matrix[0,0] = "it's a tie";
方括号中的部分是一个被评估并强制转换为字符串的表达式。表达式0,0
返回最后一个&#34;,&#34;之后的值。并且评估为0
,0,1
评估为1,依此类推。
所以:
var matrix = [];
matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
创建&#34;一维&#34;分配给每个索引的最后一个值的数组:
["rock wins", "Scissors win", "it's a tie", "Scissors win", "spock wins"]
和
matrix[3,0]
评估为
matrix[0]
因此你获得了摇滚胜利&#34;。
你想要的是:
var matrix = [[],[],[],[]];
matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
matrix[0][3] = "rock wins";
matrix[0][4] = "Spock wins";
matrix[1][0] = "Paper wins";
matrix[1][1] = "it's a tie";
matrix[1][2] = "Scissors win";
matrix[1][3] = "Lizard wins";
matrix[1][4] = "Paper wins";
matrix[2][0] = "rock wins";
matrix[2][1] = "Scissors win";
matrix[2][2] = "it's a tie";
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";
console.log(matrix[3][0]); // rock wins
您还可以使用数组文字:
var matrix = [
["it's a tie","Paper wins","rock wins","rock wins","Spock wins"],
["Paper wins","it's a tie","Scissors win","Lizard wins","Paper wins"],
["rock wins","Scissors win","it's a tie","Scissors win","spock wins"],
["rock wins"]
];