首页 > 解决方案 > 如何调整行并将其移动到右侧

问题描述

巴士座位预订:

        Col1 Col2 Col3 Col4
第 0 行 |
* * * * 第 1 行 |
* * * * 第 2 行 |
* * * * 第 3 行 |
* * * * 第 4 行 |
* * * * 第 5 行 |
* * * * 第 6 行 |
* * * * 第 7 行 |
* * * * 第 8 行 |
* * * * 第 9 行 |
* * * *       
import java.util.*;

public class SeatReservation{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);

        char[][] upuan = new char[10][4];

        for (int a = 0; a < upuan.length; a++){
            for(int b = 0; b < upuan[a].length; b++){
                upuan[a][b] = '*';
            }
        }
        while (true){
        System.out.println("Bus Seat Reservation:");
        System.out.println("\t\tCol1\tCol2\tCol3\tCol4");
        for (int a = 0; a < upuan.length; a++){
            System.out.println("Row " + a + "\t|");
            for (int b = 0; b < upuan[a].length; b++){
                System.out.print(upuan[a][b] + "\t\t");
            }
        }
        System.out.println();


        System.out.print("Enter row and colum to reserve separated by space (Enter a negative number to exit): ");
        String input = sc.nextLine();

        if(input.contains("-")){
            System.out.println("Program Exit");
            break;
        }


        int row = Character.getNumericValue(input.charAt(0));
        int column = Character.getNumericValue(input.charAt(2));

        upuan[row][column] = 'X';
        }
     }
}

标签: java

解决方案


当您打印第一个“行...”时,您正在使用println(). 这将打印该行,然后引入换行符。

尝试使用print()for 行,然后println()在 for 循环之后使用 a。


推荐阅读