Java列和行

时间:2013-08-24 03:50:44

标签: java arrays arraylist header

Java帮助。

  

编写一个程序,在屏幕上打印座位表   提示用户选择座位或价格。马克卖了座位   将价格更改为0(零)。当用户指定座位时,请确保   它是可用的。如果不可用,请通知用户并提示   他们选择座位或价格。

老师希望行是A-I,列是1-10我不能得到这个,因为我没有在我的文科学校学习这个高级java。老师也希望它输出我无法做到的舞台。

public class TheatreSeating {
    public static void main(String[] args) { 


        // Two dimensional array to hold seats
        int[10][9] seatsArray = 
                 {{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
                { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
                { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
                { 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, 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 }};
       String continueFlag = "Y";
        Scanner input = new Scanner(System.in);
        while (continueFlag.equals("Y") || continueFlag.equals("y")) {
            System.out.println("Please Select an option");
            System.out.println("1. Select Seat");
            System.out.println("2. Select Price");

            // Get selected option
            int option = input.nextInt();
            if (option == 1) {
                System.out
                        .println("Please enter the row number of seat (1-10):");
                // Subtract 1 as array starts from 0
                char row = input.nextInt() - 1;
                System.out
                        .println("Please enter the column number of seat (1-10):");
                // Subtract 1 as array starts from 0
                int column = input.nextInt() - 1;
                boolean seatAvailable = isSeatAvailable(seatsArray, row, column);
                if (seatAvailable) {
                    // Assign Seat
                    seatsArray[row][column] = 0;
                } else {
                    System.out.println("Requested seat is not available");
                }
            } else if (option == 2) {
                System.out
                        .println("Please enter the Price(10,20,30,40 or 50):");
                int price = input.nextInt();
                // Check available seat
                boolean found = false;
                for (int i = 0; i < seatsArray.length; i++) {
                    // Continue looking only if not found
                    if (found == false) {
                        for (int j = 0; j < seatsArray[i].length; j++) {
                            if (seatsArray[i][j] == price) {
                                // Assign the seat
                                seatsArray[i][j] = 0;
                                found = true;
                                // Exit from inner loop
                                break;
                            }
                        }
                    }
                }
            }
            printSeats(seatsArray);
            System.out.println("Do you want to enter more seats (Y/N)");
            continueFlag = input.next();
        }
        System.out.println("Bye");
    }

    private static void printSeats(int[][] seatsArray) {
        for (int i = 0; i < seatsArray.length; i++) {
            for (int j = 0; j < seatsArray[i].length; j++) {
                System.out.print(seatsArray[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static boolean isSeatAvailable(int[][] seatsArray, int row,
            int column) {
        for (int i = 0; i < seatsArray.length; i++) {
            for (int j = 0; j < seatsArray[i].length; j++) {
                if (i == row && j == column && seatsArray[i][j] != 0) {
                    return true;
                }
            }
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

定义引用时,不指定数组大小。

int[10][9] twoDArray; // ERROR!

使用new关键字

指定分配内存时的大小
int[][] twoDArray = new int[10][9];

但是,由于您直接初始化,因此无需在任何地方指定大小。

int[][] twoDArray = { {0,0}, {1,1} }; // same as
int[][] twoDArray = new int[][] { {0,0}, {1,1} }; // but
int[][] twoDArray = new int[10][9] { {0,0}, {1,1} }; // ERROR!

您也无法将int表达式直接分配给char。你需要一个显式的转换()让编译器知道你知道你可能会失去一些精度,因为你将int(一个更大的数据类型)填充到较小的char中数据类型。

char row = (char) (input.nextInt() - 1);

但是,你根本不应该使用char。像int一样将其更改为column

int row = input.nextInt() - 1;