2015-04-26 73 views
-1

我正在嘗試創建一個Hangman遊戲。假設猜想的單詞是「燈泡」。當用戶猜測在單詞中出現多次的字母(在本例中爲「l」)時,我想從所有位置的ArrayList中刪除該字母,以便ArrayList包含「ightbub」。以下是我處理userGuesses並檢查所選單詞中的字母的類的代碼。代碼如下:wordToGuess =(「燈泡」)ArrayList(刪除時出現問題)

另一個問題:在這段代碼下面,我已經包含了輸出。正如你所看到的,當我重複輸入同一個字母時,它說我已經猜到了一封信,即使該信應該從ArrayList中移除。

import java.util.ArrayList; 

public class InputChecker extends PromptUser{ 

static int numOfGuesses; 
static String userGuess; 
static String[] charList; 
static boolean contains; 

ArrayList<String> characters = new ArrayList<String>(); 

public boolean alive = true; 

static WordSelector ws = new WordSelector(); 
static LengthReturner lr = new LengthReturner(); 
static PromptUser pu = new PromptUser(); 

static String wordToGuess = ws.setWord(); 
static int wordLength; 

public static void finish() { 
    System.out.println(); 
    System.out.println(); 
    System.out.println("Congratulations! You have finished the game."); 
    System.out.println("Coming soon: GUI"); 
} 

public boolean askUser() { 
    while(alive == true) { 
     boolean validGuess = false; 
     while(validGuess == false) { 
      userGuess = pu.getUserInput("Enter your guess here:"); 
      if(userGuess == null || userGuess.length() > 1) { 
       validGuess = false; 
       System.out.println("Enter ONE letter!"); 
      } else { 
       validGuess = true; 
      } 
     } 
     wordLength = lr.setLength(wordToGuess); 
     charList = wordToGuess.split("(?!^)"); 
     contains = false; 
     for(String c : charList) { 
      characters.add(c); 
     } 
     for (String c : characters) { 
      if(userGuess == c) { 
       characters.remove(c); 
       contains = true; 
      } 
     } 
     if(contains == true) { 
      System.out.println("You guessed a letter!"); 
      numOfGuesses++; 
      if(characters.isEmpty()) { 
       System.out.println(); 
       System.out.println(); 
       System.out.println("Congratulations! You have successfully guessed the word, which was " + wordToGuess + "."); 
       System.out.println("You took " + numOfGuesses + " guesses!"); 
       alive = false; 
      } 
     } else { 
      System.out.println("Your guess, '" + userGuess + "', is not in the word!"); 
      numOfGuesses++; 
     } 
    } 
    return alive; 
} 
} 

下面是我重複輸入相同字母時的輸出。請注意,即使該字母應該從ArrayList中刪除,它總是說我已經成功猜出了一封信。不知何故,remove()不適合我。同樣,wordToGuess =(「燈泡:)

Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
Enter your guess here: 
l 
You guessed a letter! 
+0

如果您嘗試進行調試,您會很快找出問題所在。特別是:你應該在打印出「你猜對了一封信!」後馬上打印出'characters'。 – ruakh

回答

1

移動行:

charList = wordToGuess.split("(?!^)"); 
    contains = false; 
    for(String c : charList) { 
     characters.add(c); 
    } 

在while循環外既然是在while循環,它被重新初始化‘字符’在每次迭代中。

0

Iterator.remove()的JavaDoc的說(部分),

此方法只能一次每個呼叫被稱爲到​​。 迭代器的行爲未指定如果在迭代過程中修改了基礎集合而不是通過調用此方法

而且,你比較Object類型(包括String)與equals()(未==)平等。您需要類似於

Iterator<String> iter = characters.iterator(); 
while (iter.hasNext()) { 
    String c = iter.next(); 
    if (userGuess.equals(c)) { 
     // characters.remove(c); 
     iter.remove(); 
     contains = true; 
    } 
}