我在第66行出現(xiàn)錯(cuò)誤c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];。我手工瀏覽了索引,只是無法弄清楚索引出了什么問題。非常感謝您的幫助。package arrayproducts;import javax.swing.JOptionPane;public class ArrayProducts { public static void main(String[] args) { String output = ""; int rowA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixA.")); int colA = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixA.")); int rowB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of rows for MatrixB.")); int colB = Integer.parseInt(JOptionPane.showInputDialog("\nEnter the number of columns for MatrixB.")); if( colA != rowB){ output += "Cannot perform matrix operation: Inner matrix dimensions must agree."; output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + "."; output += "\nMatrixB has a dimension of "+ rowB + " x " + colB + "."; JOptionPane.showMessageDialog(null, output); return; } else { output += "\nDot Product Begin:"; int [][] a = new int[rowA][colA]; output += "\nMatrixA has a dimension of "+ rowA + " x " + colA + "."; int [][] b = new int[rowB][colB]; output += "\nMatrixA has a dimension of "+ rowB + " x " + colB + "."; JOptionPane.showMessageDialog(null, output); int [][] c = new int[rowA][colB]; //// // enter first matrix for(int i = 0; i < rowA; i++){ for(int j = 0; j < colA; j++){ a[i][j] = Integer.parseInt( JOptionPane.showInputDialog("\nEnter an integer for MatrixA, row " + (i+1) + " and column " + (j+1) + ".")); } }
2 回答

慕尼黑8549860
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊
我猜你打算在下面的代碼中使用索引i,j,k代替rowA,colB等。
c[rowA][colB] = c[rowA][colB] + a[rowA][colA]*b[colA][colB];

慕村9548890
TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
我將向您展示一個(gè)簡單的示例。
int[] a = new int[3];
該平均值a
只能有3個(gè)值。
a[0], a[1] and a[2]
如果嘗試a[3]
,它將超出范圍。
所以。你有
int [][] c = new int[rowA][colB];
并嘗試訪問c[rowA][colB]
超出范圍的內(nèi)容。
在你的三個(gè)for
循環(huán),我想,你想使用i
,j
和k
。
添加回答
舉報(bào)
0/150
提交
取消