2016-12-01 6 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文の効果を変更して、「もう一度選択してください」というようなものを印刷することもできます。

次に、私は実際にはダブルchar配列は必要ないと主張します。あなたはこのようにループのためにそのグラフを印刷することができます。

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アレイにアクセスできます。あなたは、アレイツールの塗りつぶしに使用することができ、今

seatNumber[selection] = 'x'; 

、すべてのOのある配列を開始するには:あなたはこのようなXになるこのインデックスで文字を変更することができ

Arrays.fill(seatNumber, 'o'); 

をあなたは意志これをコードの先頭で行うには、import java.util.Arraysにする必要があります。あなたはこれを避けたい場合は、このようなループのためにそれを埋めることができます。

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

また、あなたがScannerを宣言した後に開始するコードの周りwhile(true){のようなwhileループを書きたいことがあります。その後、次のようにプログラムを終了することができます。

これは、複数の座席を記入し、座席表を複数回表示することができます。 幸運を祈る!

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で行う必要があるのは、グラフを表示することです。チャートを表示する。グラフを表示する例:

また、ユーザーが0を選択するまで連続して実行できるように、whileループでオプションの選択肢をラップします。ここで

は、あなたの最終的なコードは次のように見て終わるべきである:

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; 
} 
関連する問題