作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上我想做的是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/
我是一名优秀的程序员,十分优秀!