Java 座位预订二维数组
我正在做一个简单的座位预订,它使用维度数组。程序应要求用户输入座位号,并将保留的替换为0。此外,不允许用户保留先前保留的座位,并应显示“座位已占用”。我有二维数组表(其他stackoverflow成员的积分帮助我解决了这个问题),现在我不知道如何将座位号更改为0。你们能告诉我怎么解决这个问题吗。谢谢 这是我的密码:Java 座位预订二维数组,java,multidimensional-array,Java,Multidimensional Array,我正在做一个简单的座位预订,它使用维度数组。程序应要求用户输入座位号,并将保留的替换为0。此外,不允许用户保留先前保留的座位,并应显示“座位已占用”。我有二维数组表(其他stackoverflow成员的积分帮助我解决了这个问题),现在我不知道如何将座位号更改为0。你们能告诉我怎么解决这个问题吗。谢谢 这是我的密码: package newtable; import java.io.*; public class Newtable { public static void print
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;
导入java.io.*;
公共类Newtable{
公共静态无效打印行(int[]行){
for(int i:行){
系统输出打印(一);
系统输出打印(“\t”);
}
System.out.println();
}
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int-twoDm[][]=新的int[5][7];
int i,j,k=1;
INTANS;
对于(i=0;i<5;i++){
对于(j=0;j<7;j++){
twoDm[i][j]=k;
k++;
}
}
for(int[]行:twoDm){
printRow(row);
}
System.out.print(“输入要预订的座位号:”);
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);
}
}
}
package-newtable;
导入java.io.*;
公共类Newtable{
公共静态无效打印行(int[]行){
for(int i:行){
系统输出打印(一);
系统输出打印(“\t”);
}
System.out.println();
}
公共静态void main(字符串[]args)引发异常{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int-twoDm[][]=新的int[5][7];
int i,j,k=1;
INTANS;
对于(i=0;i首先,我将使用1
或任何其他值来定义是否有人就座,因为int
值默认初始化为0。如果坚持使用0,则必须将整个二维数组初始化为与0
不同的值
另外,如果您的座位是由1到35的数字定义的,并且您只定义座位是否有人坐,我建议您使用布尔数组(而不是表格)。它们的值为true
和false
,在这种情况下更容易使用
boolean reservations[] = new boolean[35];
记住这一点,只要做:
reservations[seat] = true;
并且该值将被分配给由索引表示的元素。然后,要查看是否已经有人就座:
if(reservations[seat]) {
//The seat is taken because the value stored with the indexes
//is 0. Do whatever you think is correct (printing a value, for example)
//here.
}
如果您真的想使用int,我仍然鼓励您使用1作为“take”值
所以你设置了一个这样的保留值
reservations[seat] = 1;
要检查是否有人就座,过程稍有不同。您需要使用=
。在本例中(原语),它将检查两个值是否相同。(稍后,当您使用对象时,您将使用equals()
)
在所有情况下,seat
是表示用户输入的int
。您可以使用我的解决方案(即使用字符串):
公共类矩阵demo{
静态扫描仪输入=新扫描仪(System.in);
静态字符串arrS[][]=新字符串[5][5];
静态字符串cName[]={“A”、“B”、“C”、“D”、“E”};
静态int i,j;//循环控制变量
将显示数组内容的静态void dispData(){//方法
对于(i=0;iSo)当一个座位已经预订时,您想打印“预订”并且当用户试图预订一个有人坐的座位时,它会打印“有人坐”是否正确?@tkeE2036是否正确。我很难启动此程序,这似乎是一个标记问题。您的二维数组保留了座位,您想将0设置为“有人坐”,对吗?用户是如何定义座位的?当您启动程序时,已经显示了数字1-35(在表中)您将要求用户输入一个座位号,用户输入的任何内容都将变为零。程序基本上是循环的。是的,但是…1到35被转换为一对索引?要访问表中某个单元格的值,您需要提供两个索引,如twoDm[0][0]
是第0行位置0处的元素。K被设置为1,因为之前在第一组for循环中使用它时,您有K++,K的值为35。因为我再次使用它从数组开始搜索,所以必须将其设置回1。我现在已将循环添加到程序中。是否可能在再次输入viously reserved seat时,将显示“seat taked”(已占用座位)。注意:由于您知道整个多维数组的确切深度,因此可以使用伪多维技巧将k转换为i和j,如i=(k-1)/7和j=(k-1)%7;(k-1是因为您可以从1开始,而不是从零开始)我刚刚更新了我的答案,以反映已预订的支票seats@Devin我在程序中添加了一个Do while循环,而不是使用for(int l=0;l<5;l++)
,而且我还使while像这样while(true)
。你能告诉我你对我所做的事情的看法吗?谢谢!谢谢你。我将用这些代码创建另一个程序。我发现devin的答案更容易理解。再次感谢。非常感谢你的帮助。没问题,这显示了不同的人如何以不同的方式解决问题。^^。继续努力。
int reservations[] = new int[35];
reservations[seat] = 1;
if(reservations[seat] == 1) {
//The seat is taken because the value stored with the indexes
//is 0. Do whatever you think is correct (printing a value, for example)
//here.
}
public class MatrixDemo {
static Scanner input = new Scanner(System.in);
static String arrS[][] = new String[5][5];
static String cName[] = {"A","B","C","D","E"};
static int i, j; // Loop Control Variables
static void dispData() { // Method that will display the array content
for (i=0; i<5; ++i) {
for (j=0; j<5; ++j) {
System.out.print(arrS[i][j] + "\t");
}
System.out.println();
}
System.out.println();
}
static boolean chkData(String vData) { // Method that will check for reservation availability
for (i=0; i<5; ++i) {
for (j=0; j<5; ++j) {
if ((arrS[i][j]).equalsIgnoreCase(vData)) {
arrS[i][j]="X";
return true;
}
}
}
return false;
}
static boolean chkFull() { // Method that will check if all reservations were occupied
for (i=0; i<5; ++i) {
for (j=0; j<5; ++j) {
if (!(arrS[i][j]).equals("X")) {
return false;
}
}
}
return true;
}
public static void main(String eds[]) throws IOException { // the MAIN method program
String inData = new String("");
for (i=0; i<5; ++i) { // Initialized array with constant data
for (j=0; j<5; ++j) {
arrS[i][j] = new String((i+1) + cName[j]);
}
}
do { // Loop until user press X to exit
dispData();
if (chkFull())
{
System.out.println("Reservation is FULL");
inData="X";
}
else
{
System.out.print("Enter Seat Reservation: ");
inData = input.next();
if (chkData(inData))
System.out.println("Reservation Successful!");
else
System.out.println("Occupied Seat!");
}
} while (!inData.equalsIgnoreCase("X"));
}
}
// Source Code took 30 mins to finished, needs review to be able to solve faster
// Sample practice probles and codes for students.