gpt4 book ai didi

java - 循环没有在应该结束的时候结束?

转载 作者:行者123 更新时间:2023-12-01 09:21:23 24 4
gpt4 key购买 nike

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

这是类(class):

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() <=NUM_TRIES) 比较是错误的?或者也许 getNumberOfTurns 增量器位于错误的位置?我不确定。

最佳答案

改变

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

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

否则,当调用此代码时,它将设置为 5 myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);

关于java - 循环没有在应该结束的时候结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40143905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com