-1

forループ出力の空白を使用するときに出力される「null」を作成する方法はありますか?

たとえば、タイプオブジェクトの2d配列があり、配列を表示するときに、最初の3つの要素を表示してから、たとえば10個の空のスペースをスキップして、別の3つの要素を出力します。

また、ランダムジェネレーターを使用して配列要素をランダムに選択する場合、空の要素はこれに影響しますか?

主要:

public class CinemaSystem {

    public static void main(String[] args) {
        Seat cinemaactual = new Seat("Cinema");
        Seat[][] cinema = new Seat[12][23];

        Ticket ticket = new Ticket();
        Scanner scan = new Scanner(System.in);
        String answer, contiune;

        int number, check = 0, category, id;

        do {
            System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
                    + "\nWould you like to purchase tickets or list available seats?"
                    + "(/Purchase/List/Help)");
            answer = scan.nextLine();

            if (answer.equalsIgnoreCase("purchase")) {
                do {

                    System.out.println("Please choose the cateogry of ticket "
                            + "you would like, followed by who the ticket is for"
                            + "and the amount required. (separated by a space)\n"
                            + "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
                            + " Child\n3. Concession");
                    category = scan.nextInt();
                    number = scan.nextInt();
                    id = scan.nextInt();
                    if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {
                        ticket.SetType(category);
                        if (category == 1) {
                            ticket.SetName("Gold");
                        } else if (category == 2) {
                            ticket.SetName("Siler");
                        } else {
                            ticket.SetName("Bronze");
                        }
                        ticket.SetNumber(number);
                        ticket.SetID(id);
                        if (id == 1) {
                            ticket.SetCategory("Adult");
                        } else if (id == 2) {
                            ticket.SetCategory("Child");
                        } else {
                            ticket.SetCategory("Bronze");
                        }
                        System.out.print("You have selected"
                                + ticket.GetNumber() + " " + ticket.GetName()
                                + " ticket(s) at the" + ticket.GetCategory() + " price .");

                        ticket.BuyTicket(category, id, number);
                    } else {
                        System.out.print("Sorry, incorrect input, please enter an apropriate value.");
                        check = scan.nextInt();
                    }
                } while (check == 0 || check > 3);

                do {
                    System.out.print("Would you like to perchase more tickets? (Yes/No)");
                    contiune = scan.nextLine();
                } while (contiune.equalsIgnoreCase("Yes"));
            } else if (answer.equalsIgnoreCase("list")) {
                cinemaactual.CreateTheatre(cinema);
                cinemaactual.DisplayTheatre(cinema);
            } else if (answer.equalsIgnoreCase("help")) {
                // Code for help
            } else if (answer.equalsIgnoreCase("quit")) {
                System.exit(-1);
            }

            System.out.print("Sorry, incorrect input please enter"
                    + " a valid input (Purchase/List/Help or QUIT to exit");
            answer = scan.nextLine();

        } while (!answer.equalsIgnoreCase("purchase")
                || !answer.equalsIgnoreCase("List")
                || !answer.equalsIgnoreCase("help")
                || !answer.equalsIgnoreCase("quit"));
    }
}

配列を作成するクラスのメソッド:

public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 8; row++) {
    for (int col = 0; col < 4; col++) {
        x[row][col] = new Seat("B");
    }
}

for (int row = 8; row < 12; row++) {
    for (int col = 0; col < 4; col++) {
        x[row][col] = new Seat("S");
    }
}

for (int row = 0; row < 8; row++) {
    for (int col = 19; col < 23; col++) {
        x[row][col] = new Seat("B");
    }
}

for (int row = 8; row <12; row++){
    for (int col = 19; col < 23; col++) {
        x[row][col] = new Seat("S");
    }
}

for (int row = 3; row < 5; row++) {
    for (int col = 4; col < 9; col++) {
        x[row][col] = new Seat("B");
    }
}

for (int row = 3; row < 5;row++){
    for (int col = 14; col < 19; col++) {
        x[row][col] = new Seat("S");
    }
}

for (int row = 9; row < 12; row++) {
    for (int col = 7; col < 4; col++) {
        x[row][col] = new Seat("S");
    }
}

for (int row = 3; row < 5; row++) {
    for (int col = 14; col < 20; col++) {
        x[row][col] = new Seat("B");
    }
}

for (int row = 5; row < 9; row++) {
        for (int col = 4; col < 9; col++){
        x[row][col] = new Seat("S");
    }
}

for (int row = 5; row < 9; row++) {
    for (int col = 14; col < 20; col++) {
        x[row][col] = new Seat("S");
    }
}

for (int row = 6; row < 9; row++) {
    for (int col = 9; col < 14; col++) {
        x[row][col] = new Seat("G");
    }
}

for (int row = 9; row < 12; row++) {
    for (int col = 7; col < 16; col++) {
        x[row][col] = new Seat("G");
    }
}
return x;
}

配列の表示用:

public void DisplayTheatre(Seat[][] x) {
    for (int row = 0; row < x.length; row++) {
        for (int col = 0; col < x[row].length; col++) {
            System.out.print(x[row][col] + "  ");
        }
        System.out.println();
    }
}

同じ行にいくつかのオブジェクトを表示してから中央に空白スペースを表示し、さらにいくつかのオブジェクトを表示したいので、nullを削除できるかどうか疑問に思っています。

また、null要素がランダムに選択されるように、null要素が配列からランダムに要素を選択するのを妨げますか?

4

2 に答える 2

1

私があなたの要件を理解している場合。以下はあなたが探しているものです。

  String[] arr = new String[5];
       arr[0]="2";
       arr[1]="3";
       arr[4]="5";
       for(int i=0; i<arr.length; i++){
           if(arr[i]==null){
               System.out.println(' ');
           }
           else {
               System.out.println(arr[i]);
           }
       }
于 2012-10-28T11:40:44.910 に答える
0

また、ランダムジェネレーターを使用して配列要素をランダムに選択する場合、空の要素はこれに影響しますか?

一部の要素がnullの場合、要素をランダムに選択すると、必然的にnullが発生することがあります。

null以外の要素が得られるまで、新しい乱数で再試行することで、これに対処することができます。ただし、配列がまばらである場合、これはコストがかかる可能性があります。また、すべての要素がnullの場合、無限ループが発生する可能性があります。

(ただし、null以外の要素を最初のランダムな位置から検索するだけではいけません...それは偏った選択を与えるからです。)


ただし、より良い解決策は、空いている席(または何でも)だけを含む別のリストを用意することです。

そして、理想的な映画館予約システムは、ランダムに座席を割り当てることはありません。むしろ、それは整然と満たそうとします...ジムと彼のガールフレンドが最後の2枚のチケットを購入したとしても一緒に座る可能性が高いように。

于 2012-10-28T12:41:48.747 に答える