2016-03-27 8 views
0

私は速いハングマンゲームを作っていて、IndexOutOfBoundsExceptionに出くわして、なぜか不思議に思っていました。私は、このエラーがどのように起こるか、問題を見ません。IndexOutOfBoundsエラー、ハングマンゲーム

それは、この行で行わ:

array[index]+=c; 

すべてのフィードバックを歓迎です。

import java.io.IOException; 
import java.util.Scanner; 

public class Driver { 

    public static void main(String[]args) throws IOException { 

     Scanner console = new Scanner (System.in); 
     String[] phrase={"television"}; 
     String[] array= new String[phrase.length]; 
     int body =6; 
     while(array!=phrase) { 
      char c=(char)System.in.read(); 
      int index= console.nextInt(); 
      array[index]+=c; 
      if(array[index].charAt(index)==phrase[index].charAt(index)){ 
       System.out.println("the new array"); 
      } 
     } 
    } 
} 
+0

@Jensそれがここで宣言されます: 'int型のインデックス= console.nextInt (); '、質問は入力が何であるかです。 –

+0

あなたの投稿を編集し、完全なスタックトレースを含めてください。 –

+0

@Ori Lentzどういう意味ですか? –

答えて

2

コードには多くの問題があります。それらのいくつかは以下の通りです。

  • "console.nextInt();"の2を入力すると、サイズが "phrase.length"の配列が作成されます。それは範囲外のインデックスを投げるでしょう。
  • アレイ平等のチェックが間違っている、あなたは(をArrays.equals(配列1、配列2))場合

    のような何かをする必要があり

+0

君は天才に感謝している! –

関連する問題