0

我正在做一个用于维度数组的简单座位预订。程序应要求用户输入座位号并将保留的座位号替换为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());

    }
}
4

3 回答 3

1

我认为这就是你想要的:

    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++;
       }
    }
于 2012-10-11T19:42:28.450 回答
1

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.

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.

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.

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).

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.

于 2012-10-11T19:43:07.200 回答
0

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.
于 2014-02-18T03:42:59.467 回答