2016-12-01 60 views
0

我无法找到如何修改扫描仪中的字符。代码应该像'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

*“如何进行?” *你可以对正是你所面临的问题是有点更彻底? – Gendarme

回答

0

这可能是一个解决办法:

static int ROWS = 10; 
static int COLS = 3; 

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

    boolean[][] grid = new boolean[ROWS][COLS]; 

    int menu = input.nextInt(); 

    if (menu < 3) { 
     System.out.println("Which seat would you like " + (menu == 1 ? "(1-9)": "(10-30)")+": "); 
     int selection = input.nextInt(); 
     if(((menu == 1 && selection <= 9 && selection >= 1) || (menu == 2 && selection <= 30 && selection >= 10))){ 
     grid[selection/3][selection % 3] = true; 
     } 
    } else if (menu == 3) { 

    for(int r = 0 ; r < ROWS; r++){ 
     for(int c =0; c < COLS ;c++){ 
     System.out.print(grid[r][c] ? 'x' : 'o'); 
     } 
     System.out.println(); 
    } 
    } 
} 
0

首先,你可以得到该错误消息(你在评论有)由一个条件并抛出一个错误:

if (menu < 1 || menu > 3) 
    throw new IllegalArgumentException("number not between 1 and 3"); 

这会杀死程序。您还可以更改if语句的效果,只是为了打印“请再次选择”之类的内容。

接下来,我会争辩说,你实际上不需要一个双字符数组。您可以打印图表与一个循环是这样的:

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数组。您可以在此指标修改字符为x这样的:

seatNumber[selection] = 'x'; 

现在,开始使用数组是全0,则可以使用组工具填充:

Arrays.fill(seatNumber, 'o'); 

你会需要import java.util.Arrays才能在代码顶部执行此操作。如果你想避免这种情况,你可以用它填充了这样的循环:

char[] seatNumber = new char[30]; 
for (int count = 0; count < seatNumber.length; count++){ 
    seatNumber[count] = 'o'; 
} 

而且,你可能要像写的代码周围while(true){ while循环开始你声明Scanner后。然后你可以像这样退出程序:

if (menu == 0) break; 

这应该允许你填写多个席位并多次显示座位图。 祝你好运!

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.这将是最好有字符的代码创建伊始[] [],保持整个代码更新它使所有你必须在选项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(""); 
    } 
} 

而且包装您的选项中选择一个,而循环,这样就可以连续运行,直到用户选择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; 
}