我有一个类Matrix,我在应用程序类中使用this指针来访问Matrix类的方法.由于某种原因,它不能识别方法.这是一个代码片段,当我使用它时,它给我一个错误,从AddMatrix方法开始
import java.io.*;
import java.util.*;
import java.util.Scanner;
class Matrix {
double [][] element;
int rows, cols ;
Matrix(int rows, int cols){
this.rows = rows;
this.cols = cols;
element = new double [rows][cols];
}
public double getValue (int row, int col){
return element[row][col];
}
public void setValue (int row, int col, double value){
element[row][col] = value;
}
public int getNoRows(){ // returns the total number of rows
return rows;
}
public int getNoCols(){ // returns the total number of cols
return cols;
}
// The methods for the main calculations
public Matrix AddMatrix(Matrix m2){
int row1 = getNoRows();
int col1 = getNoCols();
Matrix result = new Matrix(row1, col1);
for (int i=0; i<row1; i++){
for (int j=0; j<col1; j++) {
result.setValue(i,j, (getValue(i,j) + m2.getValue(i,j)));
}
}
return result;
}
public Matrix MultiplyMatrix(Matrix m2){
if (this.getNoCols != m2.getNoRows)
throw new IllegalArgumentException ("matrices can't be multiplied");
int row2 = this.getNoRows();
int col2 = m2.getNoCols();
Matrix result = new Matrix(row2, col2);
for (int i=0; i<row2; i++){
for (int j=0; j<col2; j++){
result.setValue(i,j,(this.getValue(i,j)*m2.getValue(i,j)));
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
this.getNoCols != m2.getNoRows
应该
this.getNoCols() != m2.getNoRows()
您正在使用语法来访问变量而不是调用方法的语法.
| 归档时间: |
|
| 查看次数: |
634 次 |
| 最近记录: |