java - 卡在这个JAVA多维数组问题有一段时间了,请帮帮我
问题描述
开始时,程序应读取两个正整数,分别代表行数和每行座位数。然后,它应该打印一个包含以下三个项目的菜单:
- 显示座位应该打印当前的座位安排。空座位应标有 S 符号,占用座位应标有 B 符号。
- 买票应该从输入中读取座位坐标并像上一阶段一样打印票价。之后,当调用显示座位项时,所选座位应标有 B。
- 退出应该停止程序。
这是我到目前为止所尝试的:
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方法后数组都会重置,因此当我购买另一个座位时,程序不会显示已预订的座位。
解决方案
您需要对代码进行一些更改,
- 二维字符数组需要在静态上下文中使用,因为您使用相同的数组进行更改。
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
推荐阅读
- python - 如何对齐相邻行tkinter中的单选按钮?
- makefile - 单线程运行比 2 线程运行慢 6 倍
- azure - 如何将 Power BI 管理员角色分配给安全组?
- parsing - 我得到了 Google Drive Api: parse error with post request
- javascript - 如何将 js 文件添加到页面模板 wordpress
- mongodb - 安装 bitnami mongodb-sharded 时,我从 PVC 收到错误:没有可用于此声明的持久卷并且没有设置存储类
- git - ssh -T git@github.com: 认证成功 | sudo git pull:权限被拒绝(公钥)
- javascript - 如何解决 Javascript 中的竞争条件?
- sql - SQL SELECT 并检查第二个表中的列
- arrays - 如何将代理文件添加到字典格式