-1

this is main menu:

    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
    int inp = s.nextInt();
    do{ some code }

I want to return to the first menu after the user enters 4 here

} while(inp!=4);{
                    System.out.println("=== Logged out ===");
                }
            }

is there a method that I cant use for this problem?

Armin
  • 19
  • 1

2 Answers2

0

I don't know if i really got what you want to do, but if you want to keep showing the main menu after user choose a given option (number 4), puting it all inside a while() loop should be useful, once it will always come back to the beggining of that piece of code until a given condition be false.

while(true) {
    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
    int inp = s.nextInt();
    do { 
        some code;
    } while(inp!=4);
    System.out.println("=== Logged out ===");
}
0

Maybe this can help

static int inp;
static void mainMenu() {
    Scanner s = new Scanner(System.in);
    System.out.println("Main menu");
    System.out.print("(1) User login \n(2) Sign up \n(3) Exit \n==> ");
    inp = s.nextInt();
}

public static void main(String[] args) {
            mainMenu();
        do{ 

        }   
        while(inp!=4);{
            System.out.println("=== Logged out ===");
        }
        if(inp == 4) {
            mainMenu();
        }

}
Kaique Dias
  • 45
  • 1
  • 10