LaVOZs

The World’s Largest Online Community for Developers

'; java - How to call back to specific line of code in method? - LavOzs.Com

I am new to programming and am programming my first game. One problem I am having is I do not know how to jump to a specific line or piece of code that is not a method.

    System.out.println("\n--------------------------------------------\n");
    System.out.println("Hello customer, what would you like to do today?");
    System.out.println("1: Buy");
    System.out.println("2: Sell");
    System.out.println("3: Leave");
    System.out.println("\n--------------------------------------------\n");

    choice = myScanner.nextInt();

    if (choice==1) {
        System.out.println("\n--------------------------------------------\n");
        System.out.println("What would you like to buy?");
        System.out.println("1: Chain Armor-500 Gold.");
        System.out.println("2: Gold Broadsword-200 Gold.");
        System.out.println("3: Nevermind.");
        System.out.println("\n--------------------------------------------\n");

        choice = myScanner.nextInt();

        if (choice==1) {

        }
        else if (choice==2) {

        }
        else if (choice==3) {

        } else {

        }

I would like to jump to the part where they are asked what they would like to buy when the else statement is run.

As @JohannesKuhn mentioned, this can be achieved using a while loop:

if (choice==1) {
    while(true) {
        System.out.println("\n--------------------------------------------\n");
        System.out.println("What would you like to buy?");
        System.out.println("1: Chain Armor-500 Gold.");
        System.out.println("2: Gold Broadsword-200 Gold.");
        System.out.println("3: Nevermind.");
        System.out.println("\n--------------------------------------------\n");

        choice = myScanner.nextInt();

        if (choice==1) {
        } else if (choice==2) {
        } else if (choice==3) {
        } else {
            continue; // Skips the rest of the code, returning to the start of the loop.
        }

        break; // Breaks out of the loop, "placing" you after it's closing bracket
    }
}
Related
How do I test a private function or a class that has private methods, fields or inner classes?
How to duplicate a whole line in Vim at Eclipse?
How do I call one constructor from another in Java?
How do I generate random integers within a specific range in Java?
How can I show line numbers in Eclipse?
How to mock void methods with Mockito
How to call a method after a delay in Android
How to avoid Java code in JSP files?
How to reference a method in javadoc?
How to verify that a specific method was not called using Mockito?