如何添加“再玩一次?” java的功能 [英] How to add the "play again?" feature for java
本文介绍了如何添加“再玩一次?” java的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我的班级制作一个猜谜游戏,当你猜到正确的数字时,我需要一些帮助来在游戏结束时添加再玩功能:
Im making a guessing game for my class and I need some help for adding a "play again" feature at the end of the game when you've guessed the right number:
public class GuessingGame
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
int numtoguesses = rand.nextInt(1000) + 1;
int counter = 0;
int guess = -1;
while (guess != numtoguesses)
{
System.out.print ("|" + numtoguesses + "|" + "Guess the right number: ");
guess = input.nextInt();
counter = counter + 1;
if (guess == numtoguesses)
System.out.println ("YOU WIN MOFO!");
else if (guess < numtoguesses)
System.out.println ("You're to cold!");
else if (guess > numtoguesses)
System.out.println ("You're to hot!");
}
System.out.println ("It took you " + counter + " guess(es) to get it correct");
}
}
推荐答案
一个简单的方法是将您编写的代码移动到函数中
One simple approach would be to move the code you've written into a function
public void play() {
...
}
和 main
做类似的事情:
do {
play();
playAgain = promptUser;
} while(playAgain);
这篇关于如何添加“再玩一次?” java的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文