2016-12-05 127 views
-2

我創建在Java顯示每個座位中的二維陣列的成本的座位表:二維陣列中的Java

public class MovieTheater { 
    public static void main(String[] args) { 
     final int rows = 10; 
     final int columns = 10; 
     int i; 
     int j; 
     int[][] seating = { 
      { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, 
      { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, 
      { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, 
      { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
      { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
      { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
      { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, 
      { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 }, 
      { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 }, 
      { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 } 
     }; 

然而,當我嘗試打印陣列:

 for (i = 0; i < rows; i++) { 
      System.out.print(rows[i]); 
      for (j = 0; j < columns; j++) { 
       System.out.print(columns[j]); 
      } 
     } 
    } 
} 

我收到一條錯誤:array required, but int found

這是我的陣列格式的問題,或者我的PR語法問題int解決方案?

+2

請在你的問題的標題更具體。它將幫助網絡中的人們找到更多相關的結果,以及SO用戶。 –

回答

1

你做columns[j],但columnsint,所以你不能像數組訪問它。同rows[i]。你應該做的是在內部循環

System.out.println(seating[i][j]); 
0

你實際上並沒有在for循環中訪問你的數組對象。

試試這個:因爲你想使用一個整數數組

for(i=0;i<rows;i++) 
{ 
    for(j=0;j<columns;j++) 
    { 
     System.out.print(seating[i][j]); 
    } 
} 
0

您的代碼不起作用。事實上,行和列是兩個整數(值10);你的陣列是seating

當編譯器編譯代碼它看到是這樣的:

for (i = 0; i < 10; i++) { 
    System.out.print(10[i]); 
    for (j = 0; j < 10; j++) { 
     System.out.print(10[j]); 
    } 
} 

這是不可能的。 你真正想要的是:

for (i = 0; i < rows; i++) { 
    for(j = 0; j < columns; j++) { 
     System.out.print(seating[i][j]); 
    } 
} 
1

「列」和「行」已經被定義爲int,而不是int類型的數組。行和列的索引值可用於訪問數組的行和列(就座)。它可以打印一個打印語句:

for (i = 0; i < rows; i++) for (j = 0; j < columns; j++) System.out.print(seating[i][j]);