2015-04-21 58 views
0

這是我寫的代碼,但是當我運行它時,它不會給我玩遊戲的機會。 它應該像Hang子手遊戲一樣工作。HangMan遊戲 - 遊戲在輸入所需單詞後直接結束

這是班級代碼。

package assignment2q4; 

import java.util.ArrayList; 

public class Assignment2Q4 
{ 
    private final ArrayList<Character> guessList = new ArrayList<>(); 
    private final char[] charArray; 
    private final String secretWord; 
    private int guesses; 

    public Assignment2Q4(String word) 
    { 
     guesses = 0; 
     secretWord = word; 
     charArray = secretWord.toCharArray(); 
     // TODO code application logic here 
    } 

    public void makeGuess(char c) 
    { 
     guessList.add(c); 
     guesses += 1; 
    } 

    public int getGuessCount() 
    { 
     return guesses; 
    } 

    public String getSecretWord() 
    { 
     return secretWord; 
    } 

    public String getDisguisedWord() 
    { 
     char[] disguisedWord = new char[secretWord.length()]; 
     for(int i = 0; i < secretWord.length(); i++) 
      disguisedWord[i] = '?'; 
     String aWord = ""; 
     char c; 
     for(int i = 0; i < guessList.size(); i++) 
     { 
      c = guessList.get(i); 
      for(int j = 0; j < charArray.length; j++) 
      { 
       if(c == charArray[j]) 
        disguisedWord[j] = c; 
      } 
     } 
     for(int i =0; i < secretWord.length(); i++) 
      if(disguisedWord[i] == ' ') 
       disguisedWord[i] = '?'; 
     for(int i = 0; i < secretWord.length(); i++) 
      aWord = aWord + disguisedWord[i]; 
     return aWord; 
    } 

    public boolean isFound() 
    { 
     boolean correct = true; 

     for(int i = 0; i < guessList.size(); i++) 
      if(guessList.get(i) != charArray[i]) 
       correct = false; 

     return correct; 
    } 

} 

這是我的程序代碼。

package assignment2q4; 

import java.util.Scanner; 

public class HangmanDemo 
{ 
    public static void main(String[] args) 
    { 
    String answer = " ", hiddenWord = " "; 
    String guess; 
    Scanner keyboard; 
    keyboard = new Scanner(System.in); 

    System.out.println("Enter the secret word: "); 
    answer = keyboard.next(); 

    Assignment2Q4 hangman = new Assignment2Q4(answer); 

    while(!hangman.isFound()) 
    { 
     System.out.println("Guess a letter: "); 
     guess = keyboard.next(); 

     hangman.makeGuess(guess); 

     hiddenWord = hangman.getDisguisedWord(); 
     System.out.println(hiddenWord); 
     } 
    } 
} 

輸出只是說:

「輸入密語。」

然後我輸入一個單詞,程序說它已成功運行,但沒有給我機會玩。

+1

無論如何你打算如何工作? –

+1

請指定輸出 – underdog

+0

我想讓程序執行的是玩一個Hang子手遊戲。 –

回答

4

讓我們看一下你的主要條件循環:

public boolean isFound() 
{ 
    boolean correct = true; 

    for(int i = 0; i < guessList.size(); i++) 
     if(guessList.get(i) != charArray[i]) 
      correct = false; 

    return correct; 
} 

guessList.size();當加載類的第一次,你有沒有猜測裝起來。這意味着你的尺寸是有效的0

讓我們回到你的主循環:

Assignment2Q4 hangman = new Assignment2Q4(answer); 

    while(!hangman.isFound()) 

糟糕!while(!hangman.isFound())false與空的猜測列表。你永遠不會進入你的循環!

您需要可以重新審視自己的isFound()與空單的工作,或者你需要在你的主循環do ... while()像這樣使用:

do 
    { 
     System.out.println("Guess a letter: "); 
     guess = keyboard.next(); 

     hangman.makeGuess(guess); 

     hiddenWord = hangman.getDisguisedWord(); 
     System.out.println(hiddenWord); 
     } 
    } while(!hangman.isFound()) 

祝你好運!

+1

另一種方法是從假設正確是錯誤的開始,並且只有在所有字母都暴露的情況下,纔是正確的。寫得很好的答案 – Joeblade

+0

謝謝@Joeblade,你提出了一個很好的選擇 – Mekap