gpt4 book ai didi

javascript - "How can I code a 2 dimension array input in Javascript such that it looks like this?"

转载 作者:行者123 更新时间:2023-11-28 03:34:42 25 4
gpt4 key购买 nike

基本上我想做的是Javascript中的高斯消除程序,到目前为止,当我直接在代码中输入系数矩阵时它可以工作,但现在我试图让用户输入它,我失败了。例如,如果我直接写

$A = [[1, 1, 1], [2, 1, 2], [1, 2, 3]];

该程序可以正常工作,但是,按照我输入矩阵的方法,它不会工作。

var rows= prompt("Input the number of variables");
var A = [];
alert("Input the coefficients of the equations");
for(var i = 0; i<rows; i++)
{
A.push([]);
A[i].push(new Array(rows));
for (var j = 0; j<rows; j++)
{
A[i][j]= prompt()
}
}
}

有没有办法正确编码?

最佳答案

您唯一的问题似乎是代码末尾的额外 } 。也许使用一致的段落缩进重新格式化可以在将来阐明这种错误。请参阅下面的工作代码。

var rows = prompt("Input the number of variables");

var A = [];

alert("Input the coefficients of the equations");

for(var i = 0; i<rows; i++) {
A.push([]);
A[i].push(new Array(rows));
for (var j = 0; j<rows; j++) {
A[i][j]= prompt()
}
}

console.log(A);

关于javascript - "How can I code a 2 dimension array input in Javascript such that it looks like this?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57845524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com