Florin Alexandru
Florin Alexandru

Reputation: 7

Repeat a loop already satisfied in java

I'm looking to repeat a "game" if it is already satisfied in my case where user has to guess the random number. I can't understand where to to get back to the main game unless i have to create another "do - while" loop inside it and retype the game again in the section where it says: System.out.println("you have tried: " + count + " times. Would you like to play again? y/n"). Is there a way to just bring back to the actual guess loop rather than create another one? Hopefully makes sense.

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class pass {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pass = "password123";
        String input;
        int guess;
        int count;
        count = 0;
        int num;


        do {
            System.out.print("Enter your password: ");
            input = scanner.next();

        } while (!input.equals(pass));
        System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
        do {
            num = ThreadLocalRandom.current().nextInt(1,10);
            guess = scanner.nextInt();
            count++;
            if (guess == num) {
                System.out.println(" Well done!");
                **System.out.println("you have tried: " + count + " times. Would you like to play again? y/n");**
            }
            else if (guess < num) {
                System.out.println("your number is smaller than the number given");
            }
            else {
                System.out.println("your guess is too high");
            }

        } while (guess != num);



    }
}

Upvotes: 0

Views: 76

Answers (3)

Florin Alexandru
Florin Alexandru

Reputation: 7

I manage to do this way. It seems working but one thing is letting me down at the very last when I key in "n" or other key than "y". Exception in thread "main" java.util.InputMismatchException. Is there a more softer way to finish it?

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class pass {


    public static void randomnum(){
        Scanner scanner = new Scanner(System.in);
        int guess;
        int count;
        count = 0;
        int num;
        do {
            num = ThreadLocalRandom.current().nextInt(1,10);
            guess = scanner.nextInt();
            count++;
            if (guess == num) {
                System.out.println(" Well done!");
                System.out.println("you have tried: " + count + " times.");
                String answer;
                do{
                    System.out.println("Do you want to play again? y/n");

                    answer = scanner.next();
                    if (answer.equals("y")) {
                        System.out.println("let's play again");
                        randomnum();
                        System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
                    }
                    else {
                        System.out.println("you are logout!");
                        break;
                    }

                }while (answer.equals("Y"));
                randomnum();
            }
            else if (guess < num) {
                System.out.println("your number is smaller than the number given");
            }
            else {
                System.out.println("your guess is too high");
            }

        } while (guess != num);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String pass = "password123";
        String input;



        do {
            System.out.print("Enter your password: ");
            input = scanner.next();

        } while (!input.equals(pass));
        System.out.println("Correct! Now play the guess game! Guess a number between 1 - 10.");
        randomnum();

    }
}

Upvotes: 0

MokkaCicc
MokkaCicc

Reputation: 63

If you want to reuse code you can make functions (or methods here, because we are inside a class). They can be used to encapsulate code and call it from anywhere to use it.

You can define a methods like that:

public static void methodName() {
    // code go here
}

Then, you can call it from anywhere like that :

pass.methodName(); // It will execute the code inside methodName()

In reality, this is a lot more complex than that, you can give methods values and return others, change the scope of it to make it internal only or reachable by other classes. But I presume that you are a beginner so I keep it simple. I strongly recommend you to make a quick research about Object Oriented Programmation!

For your code, you can put the game's while loop in a method and call it at the beginning and each time the player wants to restart the game. Good luck with your game!

Upvotes: 0

Amongalen
Amongalen

Reputation: 3131

The simplest solution would be to move the entire "guess loop" into a separate method. Then in the case when you want it to repeat, just call the method recursively.

Upvotes: 1

Related Questions