我无法找到如何修改扫描仪中的字符。代码应采用类似'9'的数字,并在图表中找到它的位置,将其从'o'切换为'x'。我的代码:
import java.util.Scanner;
public class a10
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
System.out.println("1 for first class \n2 for economy \n3 to view seating chart \n0 to exit");
//error message: Your choice must be a number between 0 and 3.
int menu = input.nextInt();
if (menu == 1) {
System.out.println("Which seat would you like (1-9): ");
int selection = input.nextInt();
//Seats in first class must be between 1 and 9.");
}
else if (menu == 2) {
System.out.println("Which seat would you like (10-30): ");
int selection = input.nextInt();
//Seats in economy class must be between 10 and 30.");
}
else if (menu == 3) {
//public static boolean[] seatChart (boolean seatNumber)
char[] seatNumber;
char[][] grid = new char[][]{{'o','o','o'},{'o','o','o'},{'o','o','o'}};
}
}
}
它应该如何看待
Front
x x x
x x o
o o o
x x x
x x x
x x x
x o o
o o o
x o x
o o o
Back
答案 0 :(得分:0)
这可能是一个解决方案:
tag__tag_type__name="Equipment"
答案 1 :(得分:0)
首先,您可以通过条件获取该错误消息(您在评论中有)并抛出错误:
if (menu < 1 || menu > 3)
throw new IllegalArgumentException("number not between 1 and 3");
这会杀死该程序。您还可以更改if语句的效果,只是为了打印“请再次选择”。
接下来,我认为你实际上不需要双字符数组。您可以使用for循环打印该图表:
for (int count = 0; count < seatNumber.length; count++){
System.out.print(seatNumber[count]+" ");
if (count % 3 == 0) System.out.println();
}
因此,如果您从左到右引用座位号,这可能是表示座位表的好方法。
接下来,在1或2的情况下读取的int selection
只能通过索引访问seatNumber
数组。您可以将此索引中的char修改为x,如下所示:
seatNumber[selection] = 'x';
现在,从一个全部为o的数组开始,你可以使用数组工具填充:
Arrays.fill(seatNumber, 'o');
您需要import java.util.Arrays
才能在代码顶部执行此操作。如果你想避免这种情况,你可以用这样的for循环填充它:
char[] seatNumber = new char[30];
for (int count = 0; count < seatNumber.length; count++){
seatNumber[count] = 'o';
}
此外,您可能希望在声明while(true){
之后的代码周围编写一个类似Scanner
的while循环。然后你可以像这样退出程序:
if (menu == 0) break;
这应该允许您填写多个席位并多次显示座位表。 祝你好运!
答案 2 :(得分:0)
循环遍历您的阵列,跟踪您所在的座位数。当该号码与所选座位匹配时,将值转换为“x”。
public char[][] takeSeat(char[][] grid, int seatNum) {
int currentSeat = 0;
for(int i = 0; i < grid.length; i++) {
for(int x = 0; x < grid[i].length; x++) {
currentSeat += 1;
if(currentSeat == seatNum) {
grid[i][x] = 'x';
}
}
System.out.println("");
}
return grid;
}
另外,我注意到你在选项3中创建了你的起始座位网格。最好在代码的开头创建char [] []并在整个代码中不断更新它所以你需要做的就是在选项3显示图表。显示图表。如何显示图表的示例:
else if (menu == 3) {
//public static boolean[] seatChart (boolean seatNumber)
for(int i = 0; i < grid.length; i++) {
for(int x = 0; x < grid[i].length; x++) {
System.out.print(grid[i][x]);
}
System.out.println("");
}
}
还在while循环中包装选项选项,以便您可以连续运行,直到用户选择0。
以下是您的最终代码最终应如下所示:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
char[][] grid = new char[][]{{'o','o','o'},{'o','o','o'},{'o','o','o'}};
while(true) {
System.out.println("1 for first class \n2 for economy \n3 to view seating chart \n0 to exit");
//error message: Your choice must be a number between 0 and 3.
int menu = input.nextInt();
if (menu == 1) {
System.out.println("Which seat would you like (1-9): ");
int seat = input.nextInt();
if(!(seat < 1 || seat > 9)) {
grid = takeSeat(grid, seat);
} else {
System.out.println("Not a valid seat number");
}
//Seats in first class must be between 1 and 9.");
}
else if (menu == 2) {
System.out.println("Which seat would you like (10-30): ");
int seat = input.nextInt();
if(!(seat < 10 || seat > 30)) {
grid = takeSeat(grid, seat);
} else {
System.out.println("Not a valid seat number");
}
//Seats in economy class must be between 10 and 30.");
}
else if (menu == 3) {
//public static boolean[] seatChart (boolean seatNumber)
for(int i = 0; i < grid.length; i++) {
for(int x = 0; x < grid[i].length; x++) {
System.out.print(grid[i][x]);
}
System.out.println("");
}
}
else if (menu == 0) {
break;
}
else {
System.out.println("Invalid menu option, try again");
}
}
}
public static char[][] takeSeat(char[][] grid, int seatNum) {
int currentSeat = 0;
for(int i = 0; i < grid.length; i++) {
for(int x = 0; x < grid[i].length; x++) {
currentSeat += 1;
if(currentSeat == seatNum) {
grid[i][x] = 'x';
}
}
System.out.println("");
}
return grid;
}