2016-11-14 5 views
-2

初心者ここに、このException out boundsエラーの助けが必要です。私は本当に立ち往生しています! 行列を乗算しようとしています。私はcollB未満であり、これが真である間、あなたは行列[K] [j]の境界エラーのうち、インデックスを引き起こしことができますよりも大きなJを作っている場合は、チェックしているここJava Arrayヘルプスレッド「メイン」内の例外

package arrayPractice1; 

import java.util.Arrays; 
import java.util.Scanner; 

public class Array { 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Type how much rows will matrix A have"); 
     int rowA = in.nextInt(); 
     System.out.println("Type how much columns will matrix A have"); 
     int colA = in.nextInt(); 
     System.out.println("Type how much rows will matrix B have"); 
     int rowB = in.nextInt(); 
     System.out.println("Type how much columns will matrix B have"); 
     int colB = in.nextInt(); 

     int[][] matrixA = new int[rowA][colA]; 
     int[][] matrixB = new int[rowB][colB]; 

     for (int i = 0; i < matrixA.length; i++) { 
      for (int j = 0; j < matrixA[i].length; j++) { 
       System.out.println("Enter value for row " + i + " and column " + j); 
       matrixA[i][j] = in.nextInt(); 
      } 
     } 

     for (int i = 0; i < matrixB.length; i++) { 
      for (int j = 0; j < matrixB[i].length; j++) { 
       System.out.println("Enter value for row " + i + " and column " + j); 
       matrixB[i][j] = in.nextInt(); 
      } 
     } 

     in.close(); 

     int[][] multiply = new int[colA][rowB]; 
     int sum = 0; 

     System.out.println("Mult. of matrixA and matrixB"); 

     for (int i = 0; i < rowA; i++) { 
      for (int j = 0; i < colB; j++) { 
       for (int k = 0; k < rowA; k++) { 
        sum = sum + matrixA[i][k] * matrixB[k][j]; 
       } 
       multiply[i][j] = sum; 
       sum = 0; 
      } 
     } 

     System.out.println(Arrays.deepToString(multiply)); 

    } 

} 
+0

何行ですか?それをデバッグするために何をしましたか? – Carcigenicate

答えて

0
for (int i = 0; i < rowA; i++) { 
     for (int j = 0; i < collB; j++) { 
      for (int k = 0; k < rowA; k++) { 
       sum = sum + matrixA[i][k] * matrixB[k][j]; 
      } 
      multiply[i][j] = sum; 
      sum = 0; 
     } 
    } 

助けてください。

関連する問題