首页 > 解决方案 > 卡在这个JAVA多维数组问题有一段时间了,请帮帮我

问题描述

开始时,程序应读取两个正整数,分别代表行数和每行座位数。然后,它应该打印一个包含以下三个项目的菜单:

这是我到目前为止所尝试的:

import java.util.Scanner;

public class Main {

  static int rowNum;
  static int seatNum;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number of rows:");
    int rows = scanner.nextInt();
    System.out.println("Enter the number of seats in each row:");
    int seats = scanner.nextInt();


    boolean flag = true;
    while (flag != false) {
      System.out.println("1. Show the seats");
      System.out.println("2. Buy a ticket");
      System.out.println("0. Exit");

      int option = scanner.nextInt();

      switch (option) {
      case 1:
        showSeats(rows, seats, rowNum, seatNum);
        break;

      case 2:
        buyTicket(rows, seats);
        break;

      case 0:
        flag = false;
        break;
      }
    }

  }


private static void showSeats(int rows, int seats, int rowNum, int seatNum) {
    char[][] cinemaHall = new char[rows+1][seats+1];
    System.out.println("Cinema:");
    char count = '1';

    for (int i = 0; i <= rows; i++) {
        for (int j = 0; j <= seats; j++) {
            cinemaHall[i][j] = 'S';
        }
    }

    for (int i = 0; i < 1; i++) {
        for (int j = 1; j <= seats; j++) {
            cinemaHall[0][0] = ' ';
            cinemaHall[0][j] = count;
            count++;
        }
        count = '1';
    }
    for (int i = 1; i <= rows; i++) {
        for (int j = 0; j < 1; j++) {
            cinemaHall[i][j] = count;
            count++;

        }
    }

    if (cinemaHall[rowNum][seatNum] == 0) {
      for (int i = 0; i <= rows; i++) {
        for (int j = 0; j <= seats; j++) {
            System.out.print(cinemaHall[i][j] + " ");
        } System.out.println();
    }
    } else {
      char b = 'B';
      cinemaHall[rowNum][seatNum] = b;
      for (int i = 0; i <= rows; i++) {
          for (int j = 0; j <= seats; j++) {
            System.out.print(cinemaHall[i][j] + " ");
          } System.out.println();
      }
    }
  }

private static void buyTicket(int rows, int seats) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a row number:");
    int rowNum1 = scanner.nextInt();
    rowNum = rowNum1;
    System.out.println("Enter a seat number in that row:");
    int seatNum1 = scanner.nextInt();
    seatNum = seatNum1;

    int first = rows / 2;
    int ticketPrice = 0;

    if (rows * seats < 60) {
        ticketPrice = 10;
    } else if (rowNum <= first) {
        ticketPrice = 10;
    } else {
        ticketPrice = 8;
    }
    System.out.println("Ticket price: $" + ticketPrice);
    System.out.println();

  }
}

预期输出:

Enter the number of rows:
> 7
Enter the number of seats in each row:
> 7

1. Show the seats
2. Buy a ticket
0. Exit
> 1

Cinema:
  1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S S S S
5 S S S S S S S
6 S S S S S S S
7 S S S S S S S

1. Show the seats
2. Buy a ticket
0. Exit
> 2

Enter a row number:
> 4
Enter a seat number in that row:
> 5
Ticket price: $10

1. Show the seats
2. Buy a ticket
0. Exit
> 1

Cinema:
  1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S B S S
5 S S S S S S S
6 S S S S S S S
7 S S S S S S S

1. Show the seats
2. Buy a ticket
0. Exit
> 2

Enter a row number:
> 6
Enter a seat number in that row:
> 6
Ticket price: $10

1. Show the seats
2. Buy a ticket
0. Exit
> 1

Cinema:
  1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S B S S
5 S S S S S S S
6 S S S S S B S
7 S S S S S S S

1. Show the seats
2. Buy a ticket
0. Exit
> 0

我的输出有问题:(1)当Show the seat首次运行时,在cinemaHall [0][0]上它会打印 B,即使我试图通过 if-else 块来处理它。(2) 每次调用 Buy a ticket方法后数组都会重置,因此当我购买另一个座位时,程序不会显示已预订的座位。

标签: javamultidimensional-array

解决方案


您需要对代码进行一些更改,

  1. 二维字符数组需要在静态上下文中使用,因为您使用相同的数组进行更改。
  2. showSeats方法需要检查是否已经为特定座位设置了值,如果没有更改座位。

所以在这些更改之后,代码将如下所示,

static int rowNum;
static int seatNum;
static char[][] cinemaHall;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number of rows:");
    int rows = scanner.nextInt();
    System.out.println("Enter the number of seats in each row:");
    int seats = scanner.nextInt();
    cinemaHall = new char[rows+1][seats+1];

    boolean flag = true;
    while (flag != false) {
        System.out.println("1. Show the seats");
        System.out.println("2. Buy a ticket");
        System.out.println("0. Exit");

        int option = scanner.nextInt();

        switch (option) {
            case 1:
                showSeats(rows, seats, rowNum, seatNum);
                break;

            case 2:
                buyTicket(rows, seats);
                break;

            case 0:
                flag = false;
                break;
        }
    }

}


private static void showSeats(int rows, int seats, int rowNum, int seatNum) {
    System.out.println("Cinema:");
    char count = '1';

    for (int i = 0; i <= rows; i++) {
        for (int j = 0; j <= seats; j++) {
            if(cinemaHall[i][j]!='B'){
                cinemaHall[i][j] = 'S';
            }
        }
    }

    for (int i = 0; i < 1; i++) {
        for (int j = 1; j <= seats; j++) {
            cinemaHall[0][j] = count;
            count++;
        }
        count = '1';
    }
    for (int i = 1; i <= rows; i++) {
        for (int j = 0; j < 1; j++) {
            cinemaHall[i][j] = count;
            count++;

        }
    }

    
    char b = 'B';
    cinemaHall[rowNum][seatNum] = b;
    cinemaHall[0][0] = ' ';
    for (int i = 0; i <= rows; i++) {
        for (int j = 0; j <= seats; j++) {
            System.out.print(cinemaHall[i][j] + " ");
            } System.out.println();
        }          
    }

private static void buyTicket(int rows, int seats) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a row number:");
    int rowNum1 = scanner.nextInt();
    rowNum = rowNum1;
    System.out.println("Enter a seat number in that row:");
    int seatNum1 = scanner.nextInt();
    seatNum = seatNum1;

    int first = rows / 2;
    int ticketPrice = 0;

    if (rows * seats < 60) {
        ticketPrice = 10;
    } else if (rowNum <= first) {
        ticketPrice = 10;
    } else {
        ticketPrice = 8;
    }
    System.out.println("Ticket price: $" + ticketPrice);
    System.out.println();

}

示例输出如下所示,

Enter the number of rows:
5
Enter the number of seats in each row:
5
1. Show the seats
2. Buy a ticket
0. Exit
1
Cinema:
  1 2 3 4 5 
1 S S S S S 
2 S S S S S 
3 S S S S S 
4 S S S S S 
5 S S S S S 
1. Show the seats
2. Buy a ticket
0. Exit
2
Enter a row number:
1
Enter a seat number in that row:
1
Ticket price: $10

1. Show the seats
2. Buy a ticket
0. Exit
1
Cinema:
  1 2 3 4 5 
1 B S S S S 
2 S S S S S 
3 S S S S S 
4 S S S S S 
5 S S S S S 
1. Show the seats
2. Buy a ticket
0. Exit
2
Enter a row number:
2
Enter a seat number in that row:
1
Ticket price: $10

1. Show the seats
2. Buy a ticket
0. Exit
1
Cinema:
  1 2 3 4 5 
1 B S S S S 
2 B S S S S 
3 S S S S S 
4 S S S S S 
5 S S S S S 
1. Show the seats
2. Buy a ticket
0. Exit
0

推荐阅读