如何将此二维数组显示为表格?

时间:2013-11-11 05:31:55

标签: multidimensional-array

我需要制作一张价格不一的座位。这是我到目前为止所得到的。我在制作餐桌时遇到了麻烦。

public class theatSeat {
public static final int rows=9;
public static final int cols=10;

public static void dispBox(int[][] a){
    for (int row=0;row<rows;row++){
        System.out.print((row+1)+" ");
        for (int col=0;col<cols;col++){
            System.out.print(a[row][col]+" ");
            System.out.println();
        }
    }

}//Sets the diplayed seating box

public static void getPrice(int[][]a){
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int [] [] table = new int[rows] [cols];
    for(int row=0;row<rows;row++){
        for(int col=0;col<cols;col++){
            table[row][col]=0;
        }
    }
            dispBox(table);

}} //鳍。

1 个答案:

答案 0 :(得分:0)

在你的循环中,你将所有的值都设为0.你想要各自的随机值吗?喜欢这个

Random random = new Random();
for(int row=0;row<rows;row++){
    for(int col=0;col<cols;col++){
        table[row][col]=random.nextInt(50);
    }
}

如果您在显示时出现问题,请尝试使用

public static void dispBox(int[][] a){
    for (int row=0;row<rows;row++){
        System.out.print((row+1)+" ");

        for (int col=0;col<cols;col++){
            System.out.print(a[row][col]+" ");
        }
        System.out.println();
        // this needs to go at the end of each line.
        // You were putting it at the end of each print
    }
}