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


我不知道如何根据用户输入将存储的字符串"*“替换为"X”。
到目前为止,这是我的代码。
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]);
}
}
}
}发布于 2021-01-08 18:38:56
以便根据用户输入将存储的字符串"*“替换为"X”。您不需要初始化第二个二维数组(test1)。当您在变量row1和col1中获得来自用户的输入时,您可以做的是。只需使用它们将*替换为同一数组中的X(在本例中为"table“)。
int row1 = reader.nextInt();
int col1 = reader.nextInt();之后,您需要更新名为table的2D数组:
table[row1][col1] = "X";然后,您可以调用print函数来打印更新后的数组。这样,您将只有一个二维数组,并且将根据给定的用户输入进行更新。
发布于 2021-01-08 19:47:25
这是我的完整答案,因为我们回答的是相同的问题。
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
}https://stackoverflow.com/questions/65627180
复制相似问题