簡體   English   中英

我如何顯示飛機座位表?

[英]How would I display a Airplane seating chart?

這是我的代碼:

import java.util.Scanner;

public class AirlineReservation {

    public static void main(String[]args) {
        System.out.println("Enter n:");
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[][] airplane = new int[n][4];

        for(int i = 1; i < 4; i++) {
            System.out.printf("%3d\t", i);
        }

        for(int j = 0; j <= n; j++) {
            System.out.println((j) + " -" + "       -" + "       -" + "      -");
        } 
    }
}

我想要的是在頂部顯示四個數字(它確實如此),讓用戶輸入一些數字並且具有行數(它的行數),以及每個開放部分的“ - ”。

我不知道怎么做這個,我在第一行有一些我不想要的j int。 有人可以幫我一把嗎?

這是輸出輸出:

Enter n:
5
  1       2       3      4  
1 -       -       -      -

2 -       -       -      -

3 -       -       -      -

4 -       -       -      -

5 -       -       -      -

您的代碼中有幾個錯誤。

第一個for循環應該從1結束時開始(包括兩個),所以我添加了一個=。

for (int i = 1; i <= 4; i++) {
    System.out.printf("%3d\t", i);
}

第二個循環應該從0(包括)開始,到n(排除)或從1到n(都包括在內)結束。 所以我把它改成了以下內容。 此外,對於每一行,您必須提供所有座位(4)。 所以這個新代碼:

for (int row = 1; row <= n; row++) {
    System.out.print(j); // Print the number of row
    for (int seat = 1; seat <= 4; seat++) {
        System.out.print(" -"); // Print each seat
    }
    System.out.println(""); // Go to the next line
}

但這只會打印空面。 最好的應該是打印當前的平面配置(對於空座位,x表示非空座位)。 可以使用以下代碼假設飛機[行] [座位]對於空閑座位為0而對於占用座位而言為不同值:

for (int row = 1; row <= n; row++) {
    System.out.print(j); // Print the number of row
    for (int seat = 1; seat <= 4; seat++) {
        System.out.print(" "); // Print separator
        if (airplane[row - 1][seat - 1] == 0) {
            System.out.print("-");
        } else {
            System.out.print("X");
        }
    }
    System.out.println(""); // Go to the next line
}

只需控制你的fomatting:

public static void main(String[]args) {
    System.out.println("Enter n:");
    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    int[][] airplane = new int[n][4];

    for(int i = 1; i <= 4; i++) {
        System.out.printf("\t%d\t", i);
    }
    System.out.println("");

    for(int j = 1; j <= n; j++) {
        System.out.printf("%d", j);
        for (int k = 1; k <= 4; k++) {
            System.out.print("\t-\t");
        }
        System.out.println("");
    } 
} 

可能有一個很干凈的方法,但它確實產生了你想要的輸出。

結果:

在此輸入圖像描述

重寫你編碼為:

import java.util.Scanner;

public class AirlineReservation {

    public static void main(String[]args) {
        System.out.println("Enter n:");
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[][] airplane = new int[n][4];

        for(int i = 1; i <= 4; i++) {//Change here.
            System.out.printf("%3d\t", i);
        }
        System.out.println();//Change here.

        for(int j = 1; j <= n; j++) {
            System.out.println((j) + " -" + "       -" + "       -" + "       -");//change Here.
        } 
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM