java 座位预订二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12847024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seat Reservation two dimensional array
提问by kix
I'm doing a Simple seat reservation which uses to dimensional arrays. The program should asked the user to input a seat number and replaced the reservedwith 0 also user is not allowed to reserve a previously reserved seat and should displayed "seat taken". I have the two dimensional array table (credits to other stackoverflow members which help me through this) and now i don't have the idea how to change the seat number to 0. Could you guys give me some ideas how to work this out. thanks!
我正在做一个使用维度数组的简单座位预订。程序应该要求用户输入一个座位号并将保留的座位替换为0,用户也不允许保留以前预定的座位,并应显示“已占座”。我有二维数组表(感谢其他 stackoverflow 成员帮助我解决了这个问题),现在我不知道如何将座位号更改为 0。你们能给我一些如何解决这个问题的想法吗?谢谢!
here is my code:
这是我的代码:
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());
}
}
回答by Devin
I think this is what you want:
我想这就是你想要的:
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);
}
}
}
This code searches for the seat number that has just been entered from the console and sets it to 0;
此代码搜索刚刚从控制台输入的座位号并将其设置为0;
k = 1;
for(i=0;i<5;i++) {
for(j=0;j<7;j++) {
if (k == ans) {
twoDm[i][j]= 0;
}
k++;
}
}
回答by Fritz
First of all, I would use 1
or any other value to define if a seat is taken, since int
values are by default initialized to 0. If you insist in using 0 you'll have to initialize your whole two dimentional array to a value different than 0
.
首先,我会使用1
或 任何其他值来定义是否有座位,因为int
值默认初始化为 0。如果您坚持使用 0,则必须将整个二维数组初始化为一个不同的值0
.
Also, if your seats are defined by a number from 1 to 35 and you only define if a seat is taken or not, I suggest you use an array (not a table) of booleans. They take the values true
and false
and are easier to use in this kind of situations.
此外,如果您的座位由 1 到 35 之间的数字定义,并且您只定义是否有座位,我建议您使用布尔数组(而不是表格)。它们采用值true
并且false
在这种情况下更容易使用。
boolean reservations[] = new boolean[35];
with that in mind, just do:
考虑到这一点,只需执行以下操作:
reservations[seat] = true;
And the value will be assigned to the element represented by the index. Then, to consult if a seat is already taken:
并且该值将分配给由索引表示的元素。然后,要咨询是否已经有人坐下:
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.
}
If you trylly want to use ints, I'll still encourage you to use 1 as the "taken" value.
如果您尝试使用整数,我仍然鼓励您使用 1 作为“已取”值。
int reservations[] = new int[35];
So you set a reserved value like this
所以你设置了一个这样的保留值
reservations[seat] = 1;
To check if a seat is taken, the process is slightly different. You'll need to use ==
. In this case (primitives) It'll check if both values are the same. (Later, when you use objects you'll want to use equals()
instead).
要检查是否有座位,过程略有不同。你需要使用==
. 在这种情况下(原语)它会检查两个值是否相同。(稍后,当您使用您想要使用的对象时equals()
)。
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.
}
In all of the cases, seat
is the int
that represents the user's input.
在所有情况下,seat
是int
代表用户输入的 。
回答by Edwin S. Garcia
YOU COULD USE MY SOLUTION (ie using STRING):
你可以使用我的解决方案(即使用字符串):
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.