java执行数组计算

时间:2014-06-16 05:49:01

标签: java

下面的代码将打印两个方形矩阵,我需要它们在两个矩阵之间执行乘法,但我似乎无法使该部分工作。我在问题所在的代码块之前发表了评论。但是现在所有打印都是零。我一直在网上看很多网站,但似乎无法让我的工作。

 import java.util.*;
    import java.math.*;

    public class Main {

    public static void main(String[] args) {

        //create the grid
        final int rowWidth = 9;
        final int colHeight = 9;

        Random rand = new Random();

        int [][] board = new int [rowWidth][colHeight];

        //fill the grid
        for (int row = 0; row < board.length; row++) {

            for (int col = 0; col < board[row].length; col++) {

                board[row][col] = rand.nextInt(10);
            }
        }

        //display output
        for(int i = 0; i < board.length; i++) {

            for(int j = 0; j < board[i].length; j++) {

                System.out.print(board[i][j] + " ");
                //System.out.println();
            }
            System.out.println();
        }

                System.out.println();


                int [][] board2 = new int [rowWidth][colHeight];

        //fill the grid
        for (int row2 = 0; row2 < board2.length; row2++) {

            for (int col2 = 0; col2 < board[row2].length; col2++) {

                board[row2][col2] = rand.nextInt(10);
            }
        }

        //display output
        for(int m = 0; m < board2.length; m++) {

            for(int n = 0; n < board[m].length; n++) {

                System.out.print(board[m][n] + " ");

            }
            System.out.println();
        }

    //error is somewhere here

          int[][] calculationMultiplication = new int[rowWidth][colHeight];
      for (int l = 0; l < rowWidth; l++) {
          for (int t = 0; t < colHeight; t++) {
              for (int z = 0; z < rowWidth; z++) {
                calculationMultiplication[l][t] = calculationMultiplication[l][t] + board[l][z] * board2[z][t];
              }
          }
      }    

    //display output

      System.out.println("\nProduct of the 2 matrices is ");
      for (int i = 0; i < calculationMultiplication.length; i++) {
          for (int j = 0; j < calculationMultiplication[0].length; j++) {
              System.out.print(calculationMultiplication[i][j] + " ");
          }
          System.out.println();
      }

    } //end of main
    } //end of class Main

3 个答案:

答案 0 :(得分:1)

问题是你没有填充board2数组,所以它的所有元素都是0。在for循环中,您还应该将随机值分配给board2。您正在为board数组执行两次。

//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
    for (int col2 = 0; col2 < board2[row2].length; col2++) { // notice 'board2[row].length'
        board2[row2][col2] = rand.nextInt(10);
    }
}

您应该在显示数组的for循环中执行类似的操作:

//display output
for (int m = 0; m < board2.length; m++) {
    for (int n = 0; n < board2[m].length; n++) { // notice 'board2[row].length'
        System.out.print(board2[m][n] + " ");
    }
    System.out.println();
}

答案 1 :(得分:1)

board2始终为零。

你在这里填写错误的数组:

//fill the grid
        for (int row2 = 0; row2 < board2.length; row2++) {

            for (int col2 = 0; col2 < board[row2].length; col2++) {

                board[row2][col2] = rand.nextInt(10);
            }
        }

答案 2 :(得分:0)

基本上你只是做了简单的复制粘贴错误,如果使用方法可以避免。我会告诉你如何,但首先是你如何发现你的错误:

System.out.println(String.format("%d * %d = %d",board[l][z], board2[z][t], board[l][z] * board2[z][t]));

在对calculationMultiplication[l][t]进行计算之前添加此项。如果你想要试试......

无论如何回到你的错误,你只填充了你的第一个矩阵,第二个包含零(验证你的代码在for循环中用于将随机数插入第二个矩阵),因此任何数字乘以零等于零。

我没有查看你的所有代码,因为它包含了许多让你感到困惑并且可能会混淆任何一个的复制粘贴,所以这里有一个更好的方法来避免打印矩阵和插入随机数的错误:< / p>

<强>印刷:

void printMatrix(int[][] matrix){
   for(int row =0; row < numOfRows; row++){
      for(int col =0; col < numOfCols; col++){
        System.out.print(matrix[row][col]+" ");
      }
      System.out.println(); // new line
   }
}

<强>插入

void insertMatrix(int[][] matrix){
    for(int row =0; row < numOfRows; row++){
      for(int col =0; col < numOfCols; col++){
        matrix[row][col] = rand.nextInt(10); // rand must be declared outside any method
      }
   }
}

全部放在一起:

import java.util.*;

public class Main {

    // create the grid
    final static int rowWidth = 9;
    final static int colHeight = 9;
    static Random rand;

    public static void main(String[] args) {
        rand = new Random();
        int[][] board = new int[rowWidth][colHeight];
        int[][] board2 = new int[rowWidth][colHeight];
        int[][] calculationMultiplication = new int[rowWidth][colHeight];

        // fill
        insertMatrxi(board);

        // display output
        printMatrix(board);

        System.out.println();

        // fill
        insertMatrxi(board2);

        // display output
        printMatrix(board2);

        for (int l = 0; l < rowWidth; l++) {
            for (int t = 0; t < colHeight; t++) {
                for (int z = 0; z < rowWidth; z++) {
                    calculationMultiplication[l][t] += board[l][z] * board2[z][t];
                }
            }
        }

        // display output
        System.out.println("\nProduct of the 2 matrices is ");
        printMatrix(calculationMultiplication);

    } // end of main

    public static void printMatrix(int[][] matrix) {
        for (int row = 0; row < rowWidth; row++) {
            for (int col = 0; col < colHeight; col++) {
                System.out.print(matrix[row][col] + " ");
            }
            System.out.println(); // new line
        }
    }

    public static void insertMatrxi(int[][] matrix) {
        for (int row = 0; row < rowWidth; row++) {
            for (int col = 0; col < colHeight; col++) {
                matrix[row][col] = rand.nextInt(10);
            }
        }
    }
} // end of class Main