2017-03-04 11 views
-3

このコードがあります。ava.lang.StringIndexOutOfBoundsException:文字列インデックスが範囲外にあります:9 at java.lang.String.charAt(String.java:658)

class Hangman { 
public static void main(String[] args) { 
    char LetterGuess; 
    int x; 
    int num = 0;         //Number of letters in a word 
    int y = 0; //(int)(Math.random() * 16); 
    char CurrLet; 
    String word = "yes" ; 
    byte[] Guess = new byte[15]; 

    switch(y) { 

     case 0: 
      word = "blindfold" ; 
      num = word.length(); 
      break; 
    } 
    for(boolean guessed = false; guessed = true;) { 

     for(x = 0; x < num +1; x++) { 
       if(Guess[x] == 1) { 
        CurrLet = word.charAt(x); 
        System.out.print(CurrLet); 
       } else { 
        System.out.print(" _"); 
      } 
     } 

     System.out.println(""); 
     System.out.println(""); 
     LetterGuess = Keyboard.readChar(); 

     for(x = 0; x < num +1; x++) { 
      CurrLet = word.charAt(x); 
      if(LetterGuess == CurrLet) { 
       Guess[x] = 1; 
      } 
     } 


    } 

} 

}

それは罰金コンパイルが、私は最初の文字を入力し、入力したとき、それは私に、このエラーを与える:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9 at java.lang.String.charAt(String.java:658) at Hangman.main(Hangman.java:34)

私がやろうとしていますどのような文字列をオフ文字を読まれますすべての文字を含む配列を作成する必要はありません。どんな助け?

答えて

0

変更

for(x = 0; x < num +1; x++) 

forループまた、あなたが

for(boolean guessed = false; guessed == true;) // notice == instead 

を意味し、この内trueにこのフラグを設定している場合があります(外側)

for(x = 0; x < num; x++) 

へ。

関連する問題