java中使用一维数组的矩阵程序

java中使用一维数组的矩阵程序,java,Java,我想用java编写一个矩阵乘法程序,使用一维数组,但使用二维数组值 我想知道如何编程 package group1; import java.util.Scanner; public class multiplication { public static void main(String[] args) { int m,n,p,q,sum=0,c,d,k,s; Scanner scan=new Scanner(System.in); Sy

我想用java编写一个矩阵乘法程序,使用一维数组,但使用二维数组值

我想知道如何编程

package group1;
import java.util.Scanner;
public class multiplication {
    public static void main(String[] args) {
        int m,n,p,q,sum=0,c,d,k,s;
        Scanner scan=new Scanner(System.in);
        System.out.println("enter the num of rows and columns:");
        m=scan.nextInt();
        n=scan.nextInt();

        int first[] =new int[m*n];
        System.out.println("enter the value of first matrix");
        for(c=0;c<m*n;c++){
            first[c]=scan.nextInt();
        }


        System.out.println("enter the num of rows and columns second matrix");
        p=scan.nextInt();
        q=scan.nextInt();
        int second[] = new int[p*q];
    int answer[] = new int[c];


        System.out.println("enter the elements of second matrix");
        for(c=0;c<p*q;c++){
            second[c]=scan.nextInt();
        }

        for(c=0;c<m;c++){
            for(d=0;d<n;d++)
                for(k=0;k<4;k=k+4){
                    sum=sum+first[d]*second[k];
                        //sum=sum+first[c]*second[c+3];
                }
            answer[c]=sum;
            sum=0;
        }

        System.out.println("The product is ");
        for(c=0;c<m*q;c++){
            System.out.print(answer[c] +"\t");
        System.out.println("");
        }
    }
}
我试着这样做,但我犯了错误
有人告诉我怎么做或者程序本身

你的程序不起作用,因为用来访问1D阵列的逻辑是错误的。通常,如果您想以2D方式使用1D数组,您需要执行以下操作

oneDimensionalArray[row * columns + column] 
这相当于二维数组[行,列] 也尝试使用描述性名称,如firstRows或firstColumns,而不是m或n。这样可以提高可读性。 这样,假设用户以行-列格式输入值,下面的程序使用一维数组计算矩阵积

import java.util.Scanner;

public class multiplication {
    public static void main(String[] args) {
        int firstRows, firstCols, secondRows, secondCols, c, d, k;
        Scanner scan = new Scanner(System.in);
        System.out.println("enter the num of rows and columns:");
        firstRows = scan.nextInt();
        firstCols = scan.nextInt();

        int first[] = new int[firstRows * firstCols];
        System.out.println("enter the value of first matrix");
        for (c = 0; c < firstRows * firstCols; c++) {
            first[c] = scan.nextInt();
        }

        System.out.println("enter the num of rows and columns second matrix");
        secondRows = scan.nextInt();
        secondCols = scan.nextInt();
        int second[] = new int[secondRows * secondCols];
        int answer[] = new int[firstRows * secondCols];

        System.out.println("enter the elements of second matrix");
        for (c = 0; c < secondRows * secondCols; c++) {
            second[c] = scan.nextInt();
        }
        scan.close();

        if ( firstCols != secondRows ) {
            throw new IllegalArgumentException("A:Rows: " + firstCols + " did not match B:Columns " + secondRows + ".");
        }

        for (c = 0; c < firstRows; c++) {
            for (d = 0; d < secondCols; d++) {
                for (k = 0; k < firstCols; k++) {
                    answer[(c * secondCols) + d] += first[(c * firstCols) + k] * second[(k * secondCols) + d];
                    //Equivalent to  answer[c][d] += first[c][k] * second[k][d];
                }
            }
        }

        System.out.println("The product is ");
        for (c = 0; c < firstRows; c++) {
            for (d = 0; d < secondCols; d++) {
                System.out.print(answer[(c * secondCols) + d] + "\t");
            }
            System.out.println();
        }
    }
}

我得到的错误是无用的,因为它不工作!使用更多的括号,因为在ford=0时缺少了很多括号;学习如何调试,你就会知道如何编程。