2016-10-12 17 views
0

私はコード化してからしばらく経っているので、私は自分のJavaスキルを磨いています。私は私の質問の多くの記事を見て、私が知る限り、すべてを正確に比較しているようだ。私は2つの2次元配列の要素を互いに比較していますが、それが一致すれば要素の文字を置き換えますが、それを比較しようとすると境界から外れてしまうようです。範囲外のエラーは表示されません(48行目)。2次元配列配列の要素は比較されません。配列全体を比較しようとしていません

char[][] board = new char[3][3]; 
 
char[][] player1 = new char[1][1]; 
 
char[][] player2 = new char[1][1]; 
 
int playerRow = 0; 
 
int playerCol = 0; 
 
Scanner kbd = new Scanner(System.in); 
 

 
System.out.println("Lets play a simple game of tic-tac-toe"); 
 
     System.out.println("Player 1 (X's) : Please enter a row number and column number "); 
 
     System.out.println(" in order to plot the cordinates of your desired move"); 
 
     playerRow = kbd.next().charAt(0); 
 
     playerCol = kbd.next().charAt(0); 
 
     for(int row = 0; row < board.length; row++) 
 
     { 
 
      for(int col = 0; col < board[row].length;col++) 
 
      { 
 
       if (board[row][col] == player1[playerRow][playerCol]) 
 
       { 
 
        board[row][col] = 'X'; 
 
        System.out.print(board[row][col]+" "); 
 
       } 
 
       else 
 
       { 
 
        board[row][col]= '-'; 
 
        System.out.print(board[row][col]+" "); 
 

 
       } 
 
      } 
 
      System.out.println(); 
 
     }

+0

'char'と' int'を比較しています。それは有効な比較ですが、おそらくあなたが望むことはしません。例えば、 '' 1''(char)は '1'(int)と同じではありません。 – resueman

+0

ああ、私は45分くらいそれを見ます。 –

+0

私はそれを変更しましたが、配列インデックスが境界外のエラーになってしまいました。私はプレーヤ配列のchar配列を作成しました。これは大丈夫ですか? @resueman –

答えて

0

あなたのコードは、これを解決するために間違ったアプローチをしているようです。私があなたの目的を誤解していない限り、私はplayer1またはplayer2は必要ではないと思います。すべてはboardに保存してください。プレイヤーがスポットを選ぶことができます、その後、ボードを表示一手、の例です

//initialize variables 
char[][] board = new char[3][3]; 
int playerRow = 0; 
int playerCol = 0; 

//clear the board 
for(int row = 0; row < board.length; row++){ 
    for (int col = 0; col < board[row].length; col++){ 
     board[row][col] = '-'; 
    } 
} 

Scanner kbd = new Scanner(System.in); 

System.out.println("Lets play a simple game of tic-tac-toe"); 
System.out.println("Player 1 (X's) : Please enter a row number and column number "); 
System.out.println(" in order to plot the cordinates of your desired move"); 

//get player's row and column 
playerRow = kbd.nextInt(); 
playerCol = kbd.nextInt(); 

//Change the chosen spot to be the player's character. 
board[playerRow][playerCol] = 'X'; 

//Display the board 
for(int row = 0; row < board.length; row++){ 
    for(int col = 0; col < board[row].length;col++){ 
     System.out.print(board[row][col] + " "); 
    } 
    System.out.println(); 
} 

:ここではそれがどのように見えるかの例です。

エラーが表示されるのは、'0'という文字を読み取った後に、それを配列のインデックスとして使用しようとしているためです。しかし、'0'0と同じではありません。実際には0という文字を表すユニコード値です。値は48であり、これは配列に有効なインデックスではありません。その入力を整数として取り、その値を配列に設定するだけです(ループを使わずに正しい場所を見つけることができます)。

+0

特にresuemanと@vergeAの皆さんありがとうございます。私はいつも互いの上に積み重ねられた修正のウサギの穴を追いかける私のプログラムを畳み込む傾向があります。私は文字通り私がする必要があったが、それを見ることはできませんでした。君たちありがとう。誰もが大好きです。 –

0

あなたのプレーヤー(I)2-Dアレイは整数2-DアレイにCHAR 2次元アレイを割り当てることを含みます。

char [] [] player1 = new int [board.length] [board.length]; char [] [] player2 =新しいint [board.length] [board.length];

私はそれが実行可能な初期化だとは思わない。

上記のコメントと同様に、charとintを比較しています。 したがって、文字の整数値と比較対象の変数値を比較しようとしています。

+0

ご迷惑をおかけして申し訳ございません。エラーは依然として発生します。 –

+0

あなたのボードは3 * 3の2次元配列ですが、プレイヤーは1 * 1なので、範囲外になります。 –

+0

ええ、それは私には完璧な意味があります、ここで私の論理エラーは何ですか? –

関連する問題