-1

我只是想知道有没有办法在使用 for 循环输出空格时输出“null”?

例如,我有一个类型对象的二维数组,当显示数组时,我想显示前三个元素,然后跳过 10 个空格,然后打印另外三个元素。

另外,如果我要使用随机生成器随机选择数组元素,空元素会影响这个吗?

主要的:

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();
    }
}

只是想知道我是否可以删除空值,因为我希望显示一些对象,然后在中间显示空白,然后在同一行上再显示一些。

空元素是否会干扰从数组中随机选择元素,例如是否会随机选择空元素?

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

另外,如果我要使用随机生成器随机选择数组元素,空元素会影响这个吗?

如果某些元素为空,那么随机选择一个元素有时会不可避免地给你一个空值。

您可以尝试通过再次尝试使用新的随机数来处理这个问题......直到您获得一个非空元素。但如果数组稀疏,这可能会很昂贵。如果所有元素都为空,它可能会给你一个无限循环。

(但不要只从初始随机位置搜索非空元素......因为这会给出有偏差的选择。)


然而,更好的解决方案是有一个单独的列表,其中只包含未占用的座位(或其他)。

理想的影院预订系统不会随意分配座位。相反,它会尝试有条不紊地填满……这样即使吉姆和他的女朋友买了最后两张票,他们也有很好的机会坐在一起。

于 2012-10-28T12:41:48.747 回答