首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简易公交车预订座位

简易公交车预订座位
EN

Stack Overflow用户
提问于 2021-01-08 18:18:59
回答 2查看 817关注 0票数 0

交给我的任务是确定用户的输入,每当用户输入预订座位的输入时,来自用户的特定输入存储的字符串将被替换为"X“,即"X”,表示座位已预订。我应该使用二维数组。

我不知道如何根据用户输入将存储的字符串"*“替换为"X”。

到目前为止,这是我的代码。

代码语言:javascript
复制
import java.util.Scanner;

public class ReservationSeat {
    
    public static void main(String []args){
        Scanner reader = new Scanner(System.in);
        
        String[] columns = { "Col 1",  "Col 2",  "Col 3",  "Col 4" };
        for(int i = 0; i < columns.length; i++){
            System.out.print("\t" + columns[i]);

        }
        String[] Rows = { "Row 1 ", "Row 2 ", "Row 3 ", "Row 4 ", "Row 5 ", "Row 6 ", "Row 7 ", "Row 8 ", "Row 9 ", "Row 10 "};
        String [][] table = {
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},   
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"}

        };
        System.out.println();   
        
        for(int row = 0; row < table.length; row++){
            System.out.print(Rows[row]);
            for(int col = 0; col < table[row].length; col++){
                
                System.out.print("\t" + table[row][col]);

            }

            System.out.println();
        }
        
        System.out.print("Enter row and column number to reserve separated by space (Enter a negative number to exit): ");
        int row1 = reader.nextInt();
        int col1 = reader.nextInt();
        table[row1][col1] = reader.next();
        int[][] test1 = new int[row1][col1];
        
        for (int row = 0; row < table.length; row++) {
            System.out.print(Rows[row]);
            
            for(int col = 1; col < test1[row].length; col++) {
                System.out.print("\t" + test1[row1][col1]);
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-08 18:38:56

以便根据用户输入将存储的字符串"*“替换为"X”。您不需要初始化第二个二维数组(test1)。当您在变量row1和col1中获得来自用户的输入时,您可以做的是。只需使用它们将*替换为同一数组中的X(在本例中为"table“)。

代码语言:javascript
复制
 int row1 = reader.nextInt();
 int col1 = reader.nextInt();

之后,您需要更新名为table的2D数组:

代码语言:javascript
复制
 table[row1][col1] = "X";

然后,您可以调用print函数来打印更新后的数组。这样,您将只有一个二维数组,并且将根据给定的用户输入进行更新。

票数 2
EN

Stack Overflow用户

发布于 2021-01-08 19:47:25

这是我的完整答案,因为我们回答的是相同的问题。

代码语言:javascript
复制
public class BusReservation {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         boolean response = true;
         String[] user_respo;
        String[] Rows = { "Row 1 ", "Row 2 ", "Row 3 ", "Row 4 ", "Row 5 ", "Row 6 ", "Row 7 ", "Row 8 ", "Row 9 ", "Row 10 "};
        String [][] table = {
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},   
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"},
                {"|*", "*","*", "*"}

        };
         

         
        do{ 
           System.out.println("Bus Reservation");
          String[] columns = { "Col 1",  "Col 2",  "Col 3",  "Col 4" };
        for(int i = 0; i < columns.length; i++){
            System.out.print("\t" + columns[i]);

        }

       
        System.out.println();   
        
        for(int row = 0; row < table.length; row++){
            System.out.print(Rows[row]);
            for(int col = 0; col < table[row].length; col++){
                
                System.out.print("\t" + table[row][col]);

            }

            System.out.println();
        }
        //TABLE REPEAT

        System.out.print("Enter row and column number to reserve separated by space (Enter a negative number to exit): ");
        //GET THE USER INPUT SEPARTING THE INTEGER WITH WHITE SPACE
        user_respo = sc.nextLine().split(" ");
        
        int row1 = Integer.parseInt(user_respo[0]);
        int col2 = Integer.parseInt(user_respo[1]);
        //CHECK IF IT IS A NEGATIVE NUMBER OR NOT
        if(Integer.parseInt(user_respo[0]) > 0 && Integer.parseInt(user_respo[1]) > 0){
             //MINUS 1 SINCE INDEX STARTS WITH 0
             table[row1-1][col2-1] = "X";
        }
        else{
        response = false;

        }
        
        }// END OF DO WHILE
        while(response == true);
        
        }// END MAIN
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65627180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档