我想询问用户他们是否想在完成游戏时再次玩游戏。目前,他们这样做的唯一方法是再次调用该程序;但是,我想提醒用户他们是否想在游戏结束后再玩一次。
public class Hangman
{
Random r;
GetData get;
String[] words = {"eat","what" };
String word;
boolean finished = false;
int badGuessCount=0;
boolean [] foundLetters;
String entryWord =" ";
public Hangman()
{
r = new Random();
get = new GetData();
playAGame();
}
public void playAGame()
{
word = words[r.nextInt(words.length)];
foundLetters = new boolean[word.length()];
while (!finished)
{
showGallows();
showWord();
getGuess();
checkGuess();
if (badGuessCount==6)
{
System.out.print('\u000C');
showGallows();
System.out.println("Sorry, but you lost.");
System.out.println("The word was: "+word);
finished=true;
}
}
}
public void showGallows()
{
System.out.print('\u000C');
if (badGuessCount==0)
man_0();
if (badGuessCount==1)
man_1();
if (badGuessCount==2)
man_2();
if (badGuessCount==3)
man_3();
if (badGuessCount==4)
man_4();
if (badGuessCount==5)
man_5();
if (badGuessCount==6)
completedMan();
System.out.println("\n");
}
public boolean showWord()
{
boolean goodGuess = false;
char ch = entryWord.charAt(0);
for (int lc=0; lc < word.length(); lc++)
if (foundLetters[lc]==true)
{
System.out.print(word.charAt(lc)+" ");
}
else if (word.charAt(lc)==ch)
{
System.out.print(word.charAt(lc)+" ");
foundLetters[lc] = true;
goodGuess = true;
}
else
System.out.print("_ ");
return goodGuess;
}
public void getGuess()
{
System.out.println("\n\n\nWhat letter do you want to guess?");
System.out.println("Type the whole word to guess the word.");
System.out.println("You have "+(6 - badGuessCount)+ "guess left.");
System.out.print("Enter guess");
entryWord = get.aWord();
}
public void checkGuess()
{
boolean goodGuess;
if (entryWord.length()>1)
{
if (entryWord.equals(word))
{
System.out.println("\n\nYes You won!");
finished = true;
System.out.println("close and run if you want to play again!");
String pause = get.aWord();
}
}
else
{
showGallows();
goodGuess = showWord();
if (goodGuess)
{
System.out.println("\n\n\nGood guess");
System.out.println("Press the Enter key to continue!");
String pause = get.aWord();
}
else
{
badGuessCount++;
System.out.println("\n\n\nBad guess!");
System.out.println("Press the Enter key to continue!");
String pause = get.aWord();
}
}
}
//public void completedMan()
}
如何提示用户再次播放,然后根据输入重启游戏?
答案 0 :(得分:0)
您需要将Test::Test::Test
本身放在循环中。
playAGame();
我建议使用do
{
playAGame();
} while (UserWantsToKeepPlaying());
private boolean UserWantsToKeepPlaying() {
// Ask the user if they want to keep playing
}
- do
循环,因为您将始终至少玩一次游戏,并且您(大概)不希望提示用户再次玩完玩游戏。
答案 1 :(得分:0)
while时的错误(UserWantsToKeepPlaying());我想做什么
do
{
playAGame();
} while (UserWantsToKeepPlaying());
}
public boolean UserWantsToKeepPlaying(int number)
{
for (int i = 1; i > number; i--)
if (number % i == 0 && i != number) return false;
else return true;
System.out.print("\n\n Play Again 'No' Press 0 Yes' Press 1 : ");
}