import java.util.Scanner;
public class Matrix{
private int rowNumber;
private int colNumber;
private int val;
int [][] matrix;
public Matrix(){
rowNumber = 0;
colNumber = 0;
}
public Matrix(int row, int col){
rowNumber = row;
colNumber = col;
Matrix obj = new Matrix();
matrix = new int [rowNumber][colNumber];
Scanner input = new Scanner(System.in);
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < colNumber; j++) {
System.out.println("Enter A" + (i+1) +""+ (j+1) + " : ");
matrix [i][j] = input.nextInt();
int val = matrix[i][j];
obj.setElement(rowNumber,colNumber,val);
}
}
obj.display();
}
public void setElement(int r, int c, int value){
matrix = new int [rowNumber][colNumber];
matrix[r][c] = value;
}
public int getElement(int r, int c){
matrix = new int [rowNumber][colNumber];
return matrix[r][c];
}
public void display(){
Matrix ex = new Matrix();
String str = "|\t";
for(int i=0; i<rowNumber; i++){
for(int j=0; j<colNumber ;j++){
**str += ex.getElement(i,j) + "\t";**
}
System.out.println(str + "|");
str = "|\t";
}
}
public static void main (String[] args) {
int rowNumber;
int colNumber;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of rows: ");
rowNumber = input.nextInt();
System.out.println ("Enter number of columns: ");
colNumber = input.nextInt();
Matrix obj1 = new Matrix(rowNumber,colNumber);
}
}
我無法顯示所需的矩陣。在獲取和設置方法中遇到問題。將程序輸入值寫入數組中,並將該值存儲在變量中。那麼該變量作爲參數傳遞給set方法setmethod接受vaalue並將其放入具有r row和c column的數組中,所有值都存儲在一個數組中,然後在構造函數中調用顯示方法,該構造函數使用得到方法來獲得價值。在此代碼中需要獲取設置方法的幫助
我用盡這很好,但它在輸出顯示爲0。或者大部分時間它給了arrayindexoutofbounds異常 – user2631892