2016-12-05 242 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]);