0

次元配列を使用する単純な座席予約を行っています。プログラムはユーザーに座席番号を入力するように要求し、予約済みを 0 に置き換えます。また、ユーザーは以前に予約した座席を予約することはできず、「座席が使用されました」と表示されます。私は 2 次元配列テーブルを持っています (他のスタック オーバーフロー メンバーのおかげでこれを解決できました) が、座席番号を 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

まず、値はデフォルトで 0 に初期化される1ため、またはその他の値を使用して、座席が使用されているかどうかを定義します。 .int0

また、座席が 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 を使用することをお勧めします。

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

すべての場合において、seatintユーザーの入力を表します。

于 2012-10-11T19:43:07.200 に答える
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.
于 2014-02-18T03:42:59.467 に答える