1. 程式人生 > >java實現兩個矩陣乘法 有個錯誤希望有大佬幫忙

java實現兩個矩陣乘法 有個錯誤希望有大佬幫忙

//java實現兩個矩陣相乘  有個錯誤在下邊  有沒有哪個大佬幫我看看  十分感謝

package 實驗五;

import java.util.Scanner;

public class Matrix {
	private int rows;
	private int cols;
	private double data[][];

	public Matrix() {
		rows = 0;
		cols = 0;
		double data[][] = new double[rows][cols];
	}

	public Matrix(int rows, int cols) {
		this.cols = cols;
		this.rows = rows;
		double data[][] = new double[rows][cols];
	}

	public Matrix(double data[][]) {
		this.data = data;
	}

	public double[][] getData(int row, int col) {
		this.cols = col;
		this.rows = row;
		data=new double[row][col];
		return data;
	}

	public double[][] setData(int row, int col, double value) {
		int i, j;
		System.out.println();
		System.out.println("輸入行數:");
		Scanner  s= new Scanner(System.in);
		rows = s.nextInt();
		row=rows;
		System.out.println("輸入列數:");
		cols = s.nextInt();
		col=cols;
		double data[][] = new double[rows][cols];
		for (i = 0; i < row; i++) {
			for (j = 0; j < col; j++) {
				Scanner x = new Scanner(System.in);
				System.out.print("第" + (i + 1) + "行" + "第" + (j + 1) + "列"+" ");
				value = x.nextDouble();
				data[i][j] = value;
			}
			System.out.println();
		}
		return data;
	}

	public double[][] multiply(Matrix m) {
		if (cols != m.rows)
			return null;
		else {
			double x[][] = new double[rows][m.cols];
			for (int i = 0; i < rows; i++) {
				for (int j = 0; j < m.cols; j++){
					for (int k = 0; k < cols; k++){
						x[i][j] += data[i][k] * m.data[k][j];  /*此處空指標異常 不知道有沒有大佬幫我看一下		 */  
					}
				}
			}
			return x;
		}
	}
}