Ozz
Ozz

Reputation: 23

Recall a method from a certain point

I want to rerun this method from a certain point when a condition (else) is met, I know I need to create a new class/method for it, but not sure how since all my variables stay in the main method.

import java.util.*;

public class OddsAndEvens {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("Let's play a game called Odds And Evens");

        System.out.println();

        System.out.println("What is your name? ");

        //user name
        String name = input.nextLine();

        System.out.println("Hi " + name + "Which do you choose? Odds or Evens ?");

        //user preference this is where I want to rerun the method from in else condition
        String pref = input.nextLine();


        if (pref.equals("Odds") || pref.equals("odds")) {

            System.out.println(name + " has picked odds ! Computer will be evens");

        } else if(pref.equals("Evens") || pref.equals("evens")) {
            System.out.println(name + " has picked evens! Computer will be odds");
        } else {
            System.out.println("please enter a valid answer");

        }
    }
}

Upvotes: 2

Views: 147

Answers (3)

achAmháin
achAmháin

Reputation: 4266

There are good answers here for you, but an alternative is to separate all the necessary functions into separate methods:

1) Call a getUserInput() method

2) Call a checkOption() method with the user input

3a) if the input equals your options, print your desired outcome

3b) if it doesn't equal it, call the getUserInput() method again

So something along the lines of:

public static void main(String[] args) {
    getUserInput();
}

static void getUserInput() {
    System.out.println("enter odds or evens");
    String pref = input.nextLine();
    checkOption(pref);
}

static void checkOption(String option) {
    if (option.equalsIgnoreCase("odds") || option.equalsIgnoreCase("evens")) {
        result(option);
    } else {
        getUserInput();
    }
}

static void result(String s) {
    System.out.println("You chose " + s);
}

Example

Upvotes: 1

arocketman
arocketman

Reputation: 1174

There are many ways to do this, one way is to do as Valentin said and just create a method you can recall. You could also just use a while loop that keeps on asking for the "odds" or "evens" until the user inputs one of the two. Something along the lines of:

    String pref = "";

    while(!pref.equalsIgnoreCase("odds") && !pref.equalsIgnoreCase("evens")) {
        pref = input.nextLine();
        if (pref.equals("Odds") || pref.equals("odds")) {
            System.out.println(name + " has picked odds ! Computer will be evens");

        } else if (pref.equals("Evens") || pref.equals("evens")) {
            System.out.println(name + " has picked evens! Computer will be odds");
        } else {
            System.out.println("please enter a valid answer");
        }
    }

You can shorten the code and make it cleaner by any means but you should get the main idea.

Upvotes: 1

Valentin Grégoire
Valentin Grégoire

Reputation: 1150

You don't need a new class. You can just copy your code and put it in a new method like this:

private static void playOddsAndEvens() {
    //here comes your code that was written in the main method.
}

Your class would then look like this:

public class OddsAndEvens {
    public static void main(String[] args) {
        playOddsOrEvens();
    }

    private static void playOddsAndEvens() {
        //here comes your code that was written in the main method.
    }
}

If you want to use this method in another class, you can make your method playOddsAndEvens() public instead of private.

From then on, you can call your method from anywhere you want as many times as you want.

Upvotes: 1

Related Questions