简体   繁体   English

我如何显示飞机座位表?

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

This is my code: 这是我的代码:

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) + " -" + "       -" + "       -" + "      -");
        } 
    }
}

What I want is to have four numbers display on top (which it does), have the user enter some number and have that be the number of rows (which it does), and "-" for each open section. 我想要的是在顶部显示四个数字(它确实如此),让用户输入一些数字并且具有行数(它的行数),以及每个开放部分的“ - ”。

I'm not sure how to do this and I have some of my j int on the first line which I do not want. 我不知道怎么做这个,我在第一行有一些我不想要的j int。 Can someone give me a hand with this? 有人可以帮我一把吗?

This is the output output: 这是输出输出:

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

2 -       -       -      -

3 -       -       -      -

4 -       -       -      -

5 -       -       -      -

You have a couple of errors in your code. 您的代码中有几个错误。

First for loop should start at 1 end at 4 (both included), so I added an =. 第一个for循环应该从1结束时开始(包括两个),所以我添加了一个=。

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

Second loop should start from 0 (included) and end at n (excluded) or from 1 to n (both included). 第二个循环应该从0(包括)开始,到n(排除)或从1到n(都包括在内)结束。 So I changed it to the following. 所以我把它改成了以下内容。 In addition for each row you have to present all the seats (4). 此外,对于每一行,您必须提供所有座位(4)。 So this new code: 所以这个新代码:

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
}

But this only prints the empty plane. 但这只会打印空面。 The best should be print the current plane configuration (with - for empty seat and x for non empty seat). 最好的应该是打印当前的平面配置(对于空座位,x表示非空座位)。 It is possible with the following code assuming that airplane[row][seat] is 0 for a free seat and a different value for occupied seat: 可以使用以下代码假设飞机[行] [座位]对于空闲座位为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
}

Just control your fomatting: 只需控制你的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("");
    } 
} 

There's probably a much clean way of doing this, but it does produce the output you're wanting. 可能有一个很干净的方法,但它确实产生了你想要的输出。

Results: 结果:

在此输入图像描述

Rewrite you coded as: 重写你编码为:

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