gpt4 book ai didi

java - 重新启动简单的java游戏

转载 作者:行者123 更新时间:2023-12-01 12:33:10 25 4
gpt4 key购买 nike

这是一个简单的java猜数字游戏。

游戏结束后,我想询问用户是否想再玩一次,例如:你想再玩一次吗? (回答 Y/N)如果用户输入 Y,游戏将重新开始并随机一个新数字,如果用户回答“N”,游戏将结束。这是我的代码:

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)");
}
}

最佳答案

你可以尝试这样的事情。顺便说一句,这不是一个好方法。

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");
}

您可以使用do-while以正确的方式执行此操作。

更好的方法

 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
}

关于java - 重新启动简单的java游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25784533/

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