這是我寫的代碼,但是當我運行它時,它不會給我玩遊戲的機會。 它應該像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);
}
}
}
輸出只是說:
「輸入密語。」
然後我輸入一個單詞,程序說它已成功運行,但沒有給我機會玩。
無論如何你打算如何工作? –
請指定輸出 – underdog
我想讓程序執行的是玩一個Hang子手遊戲。 –