gpt4 book ai didi

java - 如何要求用户再玩一次....猜猜游戏

转载 作者:行者123 更新时间:2023-12-02 04:57:18 24 4
gpt4 key购买 nike

我正在制作一个简单的猜谜游戏,但我不知道如何询问玩家是否想再次玩游戏。每次游戏结束时我都必须再次运行游戏,所以我想将此功能添加到它。我查看了其他一些帖子,但它们没有帮助。这是代码:

import java.util.*;
public class guessNumber{
private static Scanner userInput = new Scanner(System.in);
public guessNumber(){
System.out.println("~~~Guess Game~~~");
}
public void guessGame(){
System.out.println("Enter the maximum number:");
int maxNum = userInput.nextInt();
System.out.println("Guess a number between 0 and " + maxNum + ":");
int randomNumber = (int) (Math.random() * maxNum);
boolean gameOn = true;
int numberOfTries = 0;
while(gameOn){
boolean printOthers = true;
numberOfTries++;
int number = userInput.nextInt();
if(number > maxNum){
System.out.println("Please enter a number between 0 and " + maxNum + ".");
printOthers = false;
}
if(printOthers){
if(number == randomNumber){
System.out.println("=================================");
System.out.println("You guessed the right number xD.");
System.out.println("Your tried " + numberOfTries + " times.");
System.out.println("=================================");
}else if(number > randomNumber){
System.out.println("Try a lower number");
}else if(number < randomNumber){
System.out.println("Try a higher number");
}
else{
System.out.println("Please enter a number between 0 and " + maxNum + ".");
}
}
}

}
public static void main(String[] args){
guessNumber guess = new guessNumber();
guess.guessGame();




}
}

最佳答案

在你的主要部分使用 while 循环。在循环中首先调用你的guessGame(),然后,类似于你要求输入数字的方式,你询问他们是否想再玩一次(是/否?),如果他们说不,你就打破循环,否则你继续。 .

关于java - 如何要求用户再玩一次....猜猜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647947/

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