[英]Restart simple java game
Here is a simple java number guessing game. 这是一个简单的Java数字猜谜游戏。
After the game has finished I would like to ask the user whether he wants to play again like: Do you want to play again? 游戏结束后,我想问用户是否要再次玩,例如:您想再次玩吗? (Answer with Y/N) If the user inputs Y the game will restart and random a new number, if he reply with 'N ' the game will end.
(用Y / N回答)如果用户输入Y,则游戏将重新启动并随机分配一个新的数字,如果他回答“ N”,则游戏将结束。 Here's my code :
这是我的代码:
import static java.lang.Math.*;
import java.util.Scanner;
public class HomeWorkLoopGame1 {
public static void main(String[] args) {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random()*100);
Scanner guess1 = new Scanner (System.in);
System.out.println("To begin please input your number");
int num1 = guess1.nextInt();
if (num1<answer) {
System.out.print(num1);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your second guess");
}
else if (num1>answer){
System.out.println(num1);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your second guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num2 = guess1.nextInt();
if (num2<answer) {
System.out.print(num2);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your third guess");
}
else if (num2>answer){
System.out.println(num2);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your third guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num3 = guess1.nextInt();
if (num3<answer) {
System.out.print(num3);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your fourth guess");
}
else if (num3>answer){
System.out.println(num3);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your fourth guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num4 = guess1.nextInt();
if (num4<answer) {
System.out.print(num4);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your final guess");
}
else if (num4>answer){
System.out.println(num4);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your final guess");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
int num5= guess1.nextInt();
if (num5<answer) {
System.out.print(num5);
System.out.print(" Your answer is too small\n");
System.out.println("Game Over");
}
else if (num5>answer){
System.out.println(num5);
System.out.print("Your answer is too big\n");
System.out.println("Game Over");
}
else {
System.out.println("Congratulation! Your answer is correct! You win !");
}
System.out.println("Correct Answer is "+ answer);
Scanner userReply = new Scanner (System.in);
char reply;
System.out.println("Do you want to play again? Answer with Y/N)");
}
}
You can try something like this. 您可以尝试这样。 By the way this is not a good way to do this.
顺便说一句,这不是执行此操作的好方法。
Scanner userReply = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with Y/N)");
String answer= userReply.nextLine().toLowerCase();
if("y".equals(answer)){
main(args); // recursively call
}else {
System.out.println("game exit");
}
You can use do-while
to do it in proper way. 您可以使用
do-while
以正确的方式进行操作。
Better way 更好的方法
public static void main(String[] args) {
boolean isValid;
do {
isValid = true;
runGame();
Scanner userReply = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with (Y/N)");
String answer = userReply.nextLine().toLowerCase();
if ("y".equals(answer)) {
isValid =true;
} else {
isValid =false;
System.out.println("game exit");
}
} while (isValid);
}
public static void runGame() {
// your game logic
}
You can call the main method for the class and create an instance of the class as so: 您可以这样调用类的main方法并按如下方式创建类的实例:
HomeWorkLoopGame1 variable = new HomeWorkLoopGame1();
HomeWorkLoopGame1.main(args);
Basically, it runs the game again from scratch - as if the player were starting to play the game again for the first time. 基本上,它从头开始重新运行游戏-就像玩家第一次开始再次玩游戏一样。
It would be something like this: 就像这样:
Scannner in = new Scanner(System.in);
System.out.println("Do you want to play again? Answer with Y/N");
String answer = in.nextLine().toLowerCase();
if(answer.equals.("Y")){
HomeWorkLoopGame1 variable = new HomeWorkLoopGame1();
HomeWorkLoopGame1.main(args);
}
else{
//exit
}
However I haven't tested this so I'm not 100% sure it might work. 但是,我尚未对此进行测试,因此我不确定100%是否可行。 Best of luck
祝你好运
Edit: - Wrap the code in a for loop to repeat it numerous times rather than just once 编辑:-将代码包装在for循环中以重复多次,而不仅仅是重复一次
It is better (I think) to make a method for the play function. (我认为)最好为播放功能创建一种方法。
public static void main(String[] args) {
playGame ();
System.out.print("Enter something:");
String input = System.console().readLine();
if (input=="y"){
playGame();
} else {
System.exit(0);
}
}
private void playGame() {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random()*100);
Scanner guess1 = new Scanner (System.in);
....
System.out.println("Correct Answer is "+ answer);
Scanner userReply = new Scanner (System.in);
char reply;
System.out.println("Do you want to play again? Answer with Y/N)");
}
Just put a do-while loop inside your main method. 只需在主方法中放入一个do-while循环即可。
As the first line put: 如第一行所示:
do {
Immediately after System.out.println("Do you want to play again? Answer with Y/N)");
在
System.out.println("Do you want to play again? Answer with Y/N)");
put: 放:
while (reply=='Y' or reply=='y');
You'll need to make sure you're reading in the reply properly etc of course. 当然,您需要确定自己在阅读回复中是否正确。
You could do the whole thing in a loop: 您可以循环执行整个操作:
public static void main(String[] args) {
while (true) {
// your code goes here
System.out.println("Do you want to play again? Answer with Y/N)");
try {
char reply;
reply = (char) System.in.read(); // read a char
if (Character.toLowerCase(reply) != 'y') // when equals to Y break the loop
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
I tried not to change too much of your original code. 我尝试不对您的原始代码进行太多更改。 I added a few while loops, some comments and additional code to handle the user's response.
我添加了一些while循环,一些注释和其他代码来处理用户的响应。 I hope it helps.
希望对您有所帮助。
import static java.lang.Math.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
boolean play = true;
boolean notCorrect = true;
while (play) {
while (notCorrect) {
System.out.print("Welcome to number guessing game !\n");
System.out.println("Guess a number range from 1 to 100, You have 5 guess to win this game");
int answer = (int) (random() * 100);
Scanner guess1 = new Scanner(System.in);
//Guess 1
System.out.println("To begin please input your number");
int num1 = guess1.nextInt();
if (num1 < answer) {
System.out.print(num1);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your second guess");
} else if (num1 > answer) {
System.out.println(num1);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your second guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 2
int num2 = guess1.nextInt();
if (num2 < answer) {
System.out.print(num2);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your third guess");
} else if (num2 > answer) {
System.out.println(num2);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your third guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 3
int num3 = guess1.nextInt();
if (num3 < answer) {
System.out.print(num3);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your fourth guess");
} else if (num3 > answer) {
System.out.println(num3);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your fourth guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 4
int num4 = guess1.nextInt();
if (num4 < answer) {
System.out.print(num4);
System.out.print(" Your answer is too small\n");
System.out.println("Try again! Please input your final guess");
} else if (num4 > answer) {
System.out.println(num4);
System.out.print("Your answer is too big\n");
System.out.println("Try again! Please input your final guess");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
//Guess 5
int num5 = guess1.nextInt();
if (num5 < answer) {
System.out.print(num5);
System.out.print(" Your answer is too small\n");
System.out.println("Game Over");
} else if (num5 > answer) {
System.out.println(num5);
System.out.print("Your answer is too big\n");
System.out.println("Game Over");
} else {
System.out.println("Congratulation! Your answer is correct! You win !");
break;
}
System.out.println("Correct Answer is " + answer);
break;
} // while(correct)
Scanner userReply = new Scanner(System.in);
String reply;
System.out.println("Do you want to play again? Answer with (Y/N)");
reply = userReply.next().toLowerCase();
if (reply.equals("y")) {
play = true;
} else if (reply.equals("n")) {
play = false;
} else {
System.out.println("Invalid choice.");
break;
}
} // while(play)
} // main()
} // class Test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.