gpt4 book ai didi

java - 座位预订二维数组

转载 作者:行者123 更新时间:2023-12-02 07:31:34 26 4
gpt4 key购买 nike

我正在做一个简单的座位预订,它使用维度数组。程序应要求用户输入座位号,并将保留替换为 0,并且不允许用户保留先前保留的座位,并应显示“座位已占用”。我有二维数组表(感谢其他 stackoverflow 成员帮助我解决这个问题),现在我不知道如何将座位号更改为 0。你们能给我一些如何解决这个问题的想法吗?谢谢!

这是我的代码:

package newtable;

import java.io.*;

public class Newtable {

public static void printRow(int[] row) {
for (int i : row) {
System.out.print(i);
System.out.print("\t");
}
System.out.println();
}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int twoDm[][] = new int[5][7];
int i, j, k = 1;
int ans;

for (i = 0; i < 5; i++) {
for (j = 0; j < 7; j++) {
twoDm[i][j] = k;
k++;
}
}

for (int[] row : twoDm) {
printRow(row);
}
System.out.print("Enter a Seat number to reserve: ");
ans = Integer.parseInt(br.readLine());

}
}

最佳答案

我想这就是你想要的:

    package newtable;
import java.io.*;
public class Newtable {

public static void printRow(int[] row) {
for (int i : row) {
System.out.print(i);
System.out.print("\t");
}
System.out.println();
}

public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int twoDm[][]= new int[5][7];
int i,j,k=1;
int ans;

for(i=0;i<5;i++) {
for(j=0;j<7;j++) {
twoDm[i][j]=k;
k++;
}
}

for(int[] row : twoDm) {
printRow(row);
}

//this loop repeats the reserving process (and printing seats) 5 times
for (int l = 0; l < 5; l++) {
System.out.print("Enter a Seat number to reserve: ");
ans = Integer.parseInt(br.readLine());
k = 1;
for(i=0;i<5;i++) {
for(j=0;j<7;j++) {
if (k == ans) {
//here we check if the seat has already been reserved
if (twoDm[i][j]== 0) {
System.out.println("That seat has already been reserved");
}
//if its not reserved then reserve it
else {
twoDm[i][j]= 0;
}
}
k++;
}
}
//print updated array of seats
for(int[] row : twoDm) {
printRow(row);
}
}

}

此代码搜索刚刚从控制台输入的座位号并将其设置为 0;

    k = 1;
for(i=0;i<5;i++) {
for(j=0;j<7;j++) {
if (k == ans) {
twoDm[i][j]= 0;
}
k++;
}
}

关于java - 座位预订二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com