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要素が配列からランダムに要素を選択するのを妨げますか?