**您好,我必须在java中创建一个刽子手游戏。我不能使用数组。我的大部分代码都已完成,但我遇到了一些问题,欢迎提供一些提示。
我刚刚发现了一些我可以使用帮助的东西。在提示用户输入新的密码并使用newHangMan.setSecretWord(newWord)之后;我伪装的单词没有重置为“????” (与秘密词中的单词具有相同数量的“?”)。
我很抱歉这么长的帖子和糟糕的格式(第一次发布在这里)。 任何人都可以帮忙吗?**
这是我的班级档案:
public class HangMan
{
private String secretWord = "bigbang", disguisedWord = "";
private int guessCount = 0, missCount = 0;
public void setSecretWord(String newWord)
{
secretWord = newWord;
guessCount = 0;
missCount = 0;
int wordLength = newWord.length();
while(wordLength > 0)
{
disguisedWord = disguisedWord + "?";
wordLength--;
}
}
public String getSecretWord()
{
return secretWord;
}
public boolean isFound()
{
return secretWord.equalsIgnoreCase(disguisedWord);
}
public String getDisguisedWord()
{
return disguisedWord;
}
public int getGuessCount()
{
return guessCount;
}
public int getMissesCount()
{
return missCount;
}
public void guessCharacter(char c)
{
// int position = secretWord.indexOf(c);
boolean got_it = false;
String updateDisguised="";
for(int i=0; i < secretWord.length();i++)
{
if(c == secretWord.charAt(i))
{
updateDisguised = updateDisguised + secretWord.charAt(i);
String checkDuplicate = updateDisguised.substring(0,i);
int duplicatePos = checkDuplicate.indexOf(c);
if(duplicatePos <0)
guessCount++;
got_it = true;
}
else
{
updateDisguised = updateDisguised + disguisedWord.charAt(i);
}
}
if(got_it == false)
{
missCount++;
guessCount++;
}
disguisedWord = updateDisguised;
}
}
这是我的主要方法:
import java.util.Scanner;
public class HangManGame {
public static void main(String[] args)
{
boolean retry= true;
String retry_ans;
Scanner kb = new Scanner(System.in);
HangMan newHangMan = new HangMan();
String word = newHangMan.getSecretWord();
String input;
char guess;
newHangMan.setSecretWord(word);
System.out.println("Hangman game starts:");
do{
System.out.println("Guess this: " + newHangMan.getDisguisedWord());
System.out.println("Enter your guess character: [guess]");
input = kb.next();
guess = input.charAt(0);
newHangMan.guessCharacter(guess);
System.out.println(newHangMan.getDisguisedWord());
System.out.println("Number of guesses so far : " + newHangMan.getGuessCount());
System.out.println("NUmber of misses so far: " + newHangMan.getMissesCount());
if((newHangMan.getMissesCount()==7) || (newHangMan.isFound()))
{
System.out.println("The game is over");
System.out.println("Would you like to try again?");
retry_ans = kb.next();
if(retry_ans.equalsIgnoreCase("yes"))
{
retry = true;
System.out.println("Please enter a new secret word:");
String newWord = kb.next();
newHangMan.setSecretWord(newWord);
}
else
{
retry =false;
}
}
} while(retry == true);
}
}
答案 0 :(得分:4)
(newHangMan.isFound()=true)
应该是
newHangMan.isFound()
不要将bool与另一个bool进行比较。
=是评估boolean
。
答案 1 :(得分:2)
替换
while(retry = true);
与
while(retry);
前者是一个赋值,因此它永远不会计算为false
,尽管它应该。
答案 2 :(得分:1)
您的状态是一项任务,而不是比较,这可能是您问题的原因 - 您将retry
的值设置为true
(retry = true
)而不是而不是检查retry
的值当前是否为真(retry == true
)。
答案 3 :(得分:0)
经典java启动程序错误。检查stetement应该是
While(retry == true)