gpt4 book ai didi

java - 循环结束后如何重复循环

转载 作者:行者123 更新时间:2023-11-30 02:45:43 26 4
gpt4 key购买 nike

我正在做一项作业,要求创建一个猜谜游戏,但我已经编写了猜谜游戏的代码。我需要询问用户是否想再次玩猜谜游戏,其中我需要重新触发循环。我不确定如何在第一个循环之后重新触发,因为已经执行并完成了。

这是我的猜谜游戏代码:

import java.util.Random;
import java.util.Scanner;
public class ThreeEleven
{


public static void main (String[] args)
{
final int MAX = 100;
int answer, guess;
String again;
Scanner scan = new Scanner(System.in);
System.out.print ("I'm thinking of a number between 1 and "
+ MAX + ". Guess what it is: (or enter 0 to quit) ");
guess = scan.nextInt();

Random generator = new Random(); //Random generator. 1 to 100.
answer = generator.nextInt(MAX) +1;

//if (guess == answer){ //If guess equals answer
//System.out.println ("You got it! Good guessing!");

//}
//else if (guess == 0){ //Game ends
//System.out.println ("You have ended your game. Goodbye.");

//}

while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

if (guess > answer && guess != 0){ //If guess is higher than answer
System.out.println ("You guessed too high!");
guess = scan.nextInt();

}
else{
if (guess < answer && guess != 0){ //If guess is lower than answer
System.out.println ("You guessed too low!");
guess = scan.nextInt();
}

else if (guess == answer){ //If guess equals answer
System.out.println ("You got it! Good guessing!");
}
else if (guess == 0){ //Game ends
System.out.println ("You have ended your game. Goodbye.");
}
}
}
if (guess == answer){
System.out.println ("You got it! Good guessing!");
//System.out.println ("You guessed " + + " times");

// System.out.println ("Want to play again? (yes or no)?");
// again = scan.next();
// if (again == "yes")
// {
// System.out.print ("I'm thinking of a number between 1 and "


// + MAX + ". Guess what it is: (or enter 0 to quit) ");
//guess = scan.nextInt();
// while (again == "yes")
// {
// while (guess != answer && guess != 0){ //If guess and 0 is not answer, continue.

// if (guess > answer && guess != 0){ //If guess is higher than answer
// System.out.println ("You guessed too high!");
// guess = scan.nextInt();

// }
// else{
// if (guess < answer && guess != 0){ //If guess is lower than answer
// System.out.println ("You guessed too low!");
// guess = scan.nextInt();
// }

// else if (guess == answer){ //If guess equals answer
// System.out.println ("You got it! Good guessing!");
// }
// else if (guess == 0){ //Game ends
// System.out.println ("You have ended your game. Goodbye.");
// }
// }
// }
// if (guess == answer){
// System.out.println ("You got it! Good guessing!");
// }

// System.out.println ("Want to play again? (yes or no)?");
//again = scan.next();
//}
//}
}
}

输出:

I'm thinking of a number between 1 and 100. Guess what it is: (or enter 0 to quit) 50
You guessed too high!
40
You guessed too high!
30
You guessed too high!
20
You guessed too high!
10
You guessed too high!
1
You guessed too low!
5
You got it! Good guessing!

正如您从我的代码中的注释中看到的,我尝试复制循环,同时添加 if else 语句和一个问题来询问用户是否想再次玩。有没有更简单有效的方法来重新触发循环以使游戏再次继续?

提前谢谢

最佳答案

这是你必须做的:

boolean playAgain = true;
while(playAgain){
//your game code here
System.out.println("Type yes to play again, No to exit");
Scanner answer = new Scanner(System.in);
String s = answer.next();//this will take the answer of the user
answer.nextLine();//this will make sure it can take next input by returning new line
if(s.trim().toLowerCase().equals("yes")){
playAgain = true;//if the player wants to play again, the loop will repeat
}else{
playAgain = false;//else the loop will exit
}
}

关于java - 循环结束后如何重复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253745/

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