循环没有结束它应该?

时间:2016-10-20 01:10:17

标签: java

我的任务是制作一个秘密猜词游戏,它应该是游戏结束,并且如果角色的猜测次数达到5,则询问用户是否想要再次玩游戏。 我认为我的增量器是正确的,但也许不是......

这是班级:

public class SecretWord {
private String secretWord;
private String hintWord ;
private int numberOfTurns;

//Default Constructors
public SecretWord()
{
    hintWord = "";
    secretWord = "juice";
    for (int i = 0; i < secretWord.length(); i++)
    {
        hintWord+="*";
    }
    this.numberOfTurns = 0;
}
//Accessors
public String getSecretWord()
{
    return this.secretWord;
}
public String getHintWord()
{
    return this.hintWord;
}
public int getNumberOfTurns()
{
    return this.numberOfTurns;
}
//Mutators
public void setSecretWord ()
{
    this.secretWord = "juice";
}
public void setHintWord ()
{
    //Setting the hint word which sets the asterisks when you guess something right
    char[] correctLetters = new char[secretWord.length()];
    for (int i = 0; i<secretWord.length();i++)
    {
        hintWord+="*";
        correctLetters[i] += '*';
    }
}
public void setNumberOfTurns (int i)
{
    this.numberOfTurns = 5;
}
//Methods
public void guessLetter(char guess)
{
    String tempHintWord="";
    for (int i = 0; i < secretWord.length(); i++)
    {
        if (secretWord.charAt(i) == guess)
        {
            tempHintWord += guess;
        }
        else
        {
            tempHintWord += hintWord.charAt(i);
        }
    }
    hintWord = tempHintWord;
}

这是我的循环驱动程序:

public class SecretWordGame {

//Constant for number of tries
public static final int NUM_TRIES = 5;

public static void main (String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    //Starts game
    boolean quit = false;
    while (quit == false)

    {
        System.out.println("Welcome to the word guessing game! You have " +
                +NUM_TRIES+" tries to guess the secret word!");

        SecretWord myWord = new SecretWord();
        System.out.println("The current hint is \n"+myWord.getHintWord());

        while (myWord.getNumberOfTurns() <=NUM_TRIES)
        {
            System.out.println("Guess a lowercase letter");

            //Gets the first letter of what is entered

            char tempGuess = keyboard.nextLine().charAt(0);

            //Updates the hint by calling guess letter method

            myWord.guessLetter(tempGuess);

            System.out.println(myWord.getHintWord());

            System.out.println("Guess the secret word");

            String myGuess = keyboard.nextLine();
            //Checks correct guess
            if (myGuess.equals(myWord.getSecretWord()))
            {
                System.out.println("You win!");
                break;
            }
            else
            {
                System.out.println("Keep trying!");
            }
            myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);
        }
        //Prompts user to play again

        System.out.println("Game over! Try again?");

        String userInput = keyboard.nextLine();
        if(userInput.equalsIgnoreCase("no"))
        {
            quit = true;
        }
        else
        {
            System.out.println("Let's go again!");
        }
    }
    System.out.println("Goodbye!");
}

也许while循环(myWord.getNumberOfTurns()&lt; = NUM​​_TRIES)比较错了?或者getNumberOfTurns增量器可能位于错误的位置?我不确定。

1 个答案:

答案 0 :(得分:1)

变化

public void setNumberOfTurns (int i)
{
    this.numberOfTurns = 5;
}

public void setNumberOfTurns (int i)
{
    this.numberOfTurns = i;
}

否则,当调用此代码5

时,它将设置为myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);