0

I have created a simple project for my self, and now i am trying to integrate a code that will make the program restart when i enter "restart" in the console. To do that I have created a second method in my program that contains exactly the same code as my main method but now i want to appoint the second method which i don't know how to do. All help is appreciated! here is my code so far:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }

    }


    while(value != random);

    System.out.println("You guessed the number");   

    if(value == random){
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
        if (reset.equals("restart")){
            /*
             *I need some code here

            */
        }
    }   
}

public static void restart(String[] args){

    Scanner scanner = new Scanner(System.in);

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }

    }


    while(value != random);

    System.out.println("You guessed the number");   

    if(value == random){
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
        if (reset.equals("restart")){

        }
    }   
}
3
  • 2
    Why two methods that do the same thing? How about one method that just starts over? Also not sure about your use of the word "appoint", do you mean "call" or "run"? Dec 9, 2015 at 23:40
  • i am trying to start over without stopping the program(only in the console using user input). and yes i mean "run"
    – daniel p
    Dec 9, 2015 at 23:41
  • 1
    How about an outermost loop around the contents of main: do { ... } while (reset.equals("restart"); Dec 9, 2015 at 23:50

2 Answers 2

1

You should just call main() in "if (reset.equals("restart")){..."

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }

    }


    while(value != random);

    System.out.println("You guessed the number");   

    if(value == random){
        System.out.println("Would you like to restart?");
        String reset = scanner.nextLine();
        if (reset.equals("restart")){
            main(args); // restart you code
            return;
        }
    }   
}

or use following code:

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  do{

    System.out.println("guess the number between 1 and 9");

    int random = (int)(Math.random() * 9 + 1);

    int value = 0;
    do{
        value = scanner.nextInt();

        if (value != random){
            System.out.println("Try again");
        }    
    } 
    while(value != random);

    System.out.println("You guessed the number");       
    System.out.println("Would you like to restart?");
    String reset = scanner.nextLine();
  } 
  while(reset.equals("restart")); 
}
5
  • A recursive main function seems like a bad idea. Dec 10, 2015 at 0:12
  • Why? The main function is common static function, it's equals any other static function (just first call do java). Of course, you can create new function called work() or use do while loop, however for studing or testing not problem to use a recursive main function. It's much better then copy some code to other function. Dec 10, 2015 at 0:20
  • You should take the new Scanner out of the loop, otherwise you create multiple scanners on the same input stream. Dec 10, 2015 at 0:25
  • Why make something recursive when it doesn't need to be? Dec 10, 2015 at 0:26
  • Why it's question for author of this topic. Speaking for me, I never use recursive for this task, however may be author studing a recursive method? Dec 10, 2015 at 0:30
1

Try something like this,

In your code,

  1. you have been mess up code, and need to break down according to requirement, a
  2. Also, need to take care if user want to restart then again game will be played.

This things overcome by below code,

static int value;
static int random  = (int)(Math.random() * 9 + 1);
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {

    do{
        System.out.println("Enter Your Guesses :");
        value = scanner.nextInt();
        if(value == random){
            System.out.println("Exactly Match : " + value  + " == " + random);
            System.out.println("Would you like to restart?");
            String reset = scanner.next();
            if (reset.equals("restart")){
                restart();
            }
        }else{
            System.out.println("Try again");
        }

    }while(value != random);

    System.out.println("You guessed the number");   


}
public static void restart(){
    do{
        random = (int)(Math.random() * 9 + 1);
    }while(value == random);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.