2013-08-25 88 views
0
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的數組中,所有值都存儲在一個數組中,然後在構造函數中調用顯示方法,該構造函數使用得到方法來獲得價值。在此代碼中需要獲取設置方法的幫助

回答

2

不需要在get和set和display方法中再次創建數組。 您正在創建大量的對象unnecessarily.I已經改變了你的代碼才能正常工作
請使用此代碼

import java.util.Scanner; 

class Matrix { 

    private int rowNumber; 
    private int colNumber; 
    int[][] matrix; 

    public Matrix() { 
     rowNumber = 0; 
     colNumber = 0; 
    } 

    public Matrix(int row, int col) { 
     rowNumber = row; 
     colNumber = col; 
     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) + " : "); 
       setElement(i, j, input.nextInt()); 
      } 
     } 
     display(); 
    } 

    public void setElement(int r, int c, int value) { 
     matrix[r][c] = value; 
    } 

    public int getElement(int r, int c) { 
     return matrix[r][c]; 
     } 

    public void display() { 

     String str = "|\t"; 
     for (int i = 0; i < rowNumber; i++) { 
      for (int j = 0; j < colNumber; j++) { 
        str += 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); 
    } 
} 
+0

我用盡這很好,但它在輸出顯示爲0。或者大部分時間它給了arrayindexoutofbounds異常 – user2631892

1

你的代碼有幾個NullPointerException S,基本上是因爲你正在使用一個空的構造函數創建新Matrix對象和然後嘗試從那裏設置矩陣的屬性,這意味着您的空構造函數內沒有創建矩陣,從而導致它指向空。

試試這個(我也用Prabhaker的答案在這裏):

public Matrix(int row, int col){ 
    rowNumber = row; 
    colNumber = col; 
    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]; 
      setElement(rowNumber - 1, colNumber - 1, val); 
     } 
    } 
    display(); 
} 

public void setElement(int r, int c, int value) { 
    matrix[r][c] = value; 
} 

public int getElement(int r, int c) { 
    return matrix[r][c]; 
} 

public void display() { 
    String str = "|\t"; 
    for (int i = 0; i < rowNumber; i++) { 
     for (int j = 0; j < colNumber; j++) { 
      str += getElement(i, j) + "\t"; 
     } 
     System.out.println(str + "|"); 
     str = "|\t"; 
    } 
}` 
+0

It workssssssssssss – user2631892