Java 列和行

标签 java arrays arraylist header

Java 帮助。

Write a program that prints the seating chart on the screen and prompts the user to pick either a seat or a price. Mark sold seats by changing the price to 0(zero). When a user specifies a seat, make sure it is available. If it is not available, inform the user and prompt them to pick either a seat or a price.

老师希望行为 A-I,列为 1-10,我无法得到这个,因为我没有在我的文科学校学习这种高级 java。老师还希望它输出 Stage,但我无法做到。

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;
    }
}

最佳答案

定义引用时没有指定数组大小。

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,就像您的一样。

int row = input.nextInt() - 1;

关于Java 列和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18414733/

相关文章:

java - JSON 数组长度为-1,但我可以清楚地看到 JSON 中的对象

java - 创建类并进入框架

java - 从数组中删除维度

java - 计算 Arraylist 中具有特定属性的项目

java - 同步如何使 vector 线程安全?

java - 如何使用 java 的 ImageIO 保存优化的 png 图像?

java - 使用堆栈反转数组

arrays - 来自相应形状的数组结构的数据

arrays - 如何使用一个数组作为 ruby​​ 中另一个数组的映射然后加入它们?

java - 将 Java 文件中的 ArrayList 值映射到 JSP 文件中的 HTML 对象