座位预约二维阵列

时间:2012-10-11 19:24:54

标签: java multidimensional-array

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

    }
}

3 个答案:

答案 0 :(得分: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++;
       }
    }

答案 1 :(得分:1)

首先,我会使用1或任何其他值来定义座位是否被占用,因为int值默认初始化为0.如果你坚持使用0,你将会必须将整个二维数组初始化为不同于0的值。

另外,如果您的座位由1到35的数字定义,并且您只定义是否有座位,我建议您使用布尔数组(不是表格)。它们采用值truefalse,在这种情况下更容易使用。

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

如果您想尝试使用整数,我仍然会鼓励您使用1作为“已采用”值。

int reservations[] = new int[35];

所以你设置了这样的保留值

reservations[seat] = 1;

要检查座位是否被占用,过程略有不同。您需要使用==。在这种情况下(原语)它将检查两个值是否相同。 (稍后,当您使用对象时,您将要使用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.
}

在所有情况下,seat都是代表用户输入的int

答案 2 :(得分:0)

您可以使用我的解决方案(即使用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.