2016-12-01 25 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; 
}