繁体   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