简体   繁体   中英

Restart simple java game

Here is a simple java number guessing game.

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. 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.

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:

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. Best of luck

Edit: - Wrap the code in a for loop to repeat it numerous times rather than just once

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.

As the first line put:

do {

Immediately after 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM