Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java执行数组计算_Java - Fatal编程技术网

java执行数组计算

java执行数组计算,java,Java,下面的代码将打印两个方阵,我需要它们执行两个矩阵之间的乘法,但我似乎无法让这部分工作。我在问题所在的代码块前面加了一条注释。但现在它打印的都是零。我一直在网上浏览很多网站,但似乎无法让我的网站发挥作用 import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { //create the grid

下面的代码将打印两个方阵,我需要它们执行两个矩阵之间的乘法,但我似乎无法让这部分工作。我在问题所在的代码块前面加了一条注释。但现在它打印的都是零。我一直在网上浏览很多网站,但似乎无法让我的网站发挥作用

 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
import java.util.*;
导入java.math.*;
公共班机{
公共静态void main(字符串[]args){
//创建网格
最终整数行宽=9;
最终高度=9;
Random rand=新的Random();
int[]board=新int[rowWidth][colHeight];
//填写表格
对于(int row=0;row
问题在于您尚未填充
板2
数组,因此其所有元素都将
0
。在
for
循环中,还应将随机值分配给
板2
。您为
阵列执行了两次

//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);
    }
}

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);
            }
        }
//填充网格
对于(int row2=0;row2
基本上你只是做了一些简单的复制粘贴错误,如果你使用了这些方法,这些错误是可以避免的。我会告诉你怎么做,但首先,这里是你如何发现自己的错误:

System.out.println(String.format("%d * %d = %d",board[l][z], board2[z][t], board[l][z] * board2[z][t]));
在对
calculation乘法[l][t]
进行计算之前添加此项。如果你想试试

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

我没有看过你所有的代码,因为它包含大量的复制粘贴,这会让你感到困惑,也会让任何人感到困惑,所以这里有一个更好的方法来避免打印矩阵和插入随机数时出现错误:

打印:

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
void打印矩阵(int[][]矩阵){
for(int行=0;行
插入:

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
void insertMatrix(int[][]矩阵){
for(int行=0;行
将所有内容放在一起:

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
import java.util.*;
公共班机{
//创建网格
最终静态整数行宽=9;
最终静态高度=9;
静态随机兰德;
公共静态void main(字符串[]args){
rand=新随机数();
int[]board=新int[rowWidth][colHeight];
int[]board2=新int[rowWidth][colHeight];
int[][]计算乘法=新的int[rowWidth][colHeight];
//填满
插入材料XI(板);
//显示输出
打印矩阵(板);
System.out.println();
//填满
insertMatrxi(董事会2);
//显示输出
printMatrix(董事会2);
对于(int l=0;l