0

i need help, i have several cases and i want to return from a case to another case, but it keeps returning to the do menu, example i click 1 then 1 and then 1 and the program returns to the first case, the login case but i want to return to the visualize one

public static void main(String[] args) {
int op;
        Scanner ler = new Scanner(System.in);
        do
        {
            System.out.println("1 - Iniciar sessão");//login
            System.out.println("0 - Terminar programa");//end program
            System.out.print("OP-> ");
            op = ler.nextInt();

        switch(op)
            {
               case 1:
                   System.out.println("1 - Visualizar lista encomendas realizadas");//visualize
                   System.out.println("2 - Listar/adicionar/modificar/eliminar produtos");//modifie/remove/add product
                   System.out.println("3 - Listar/adicionar/modificar/eliminar clientes");//modifie/remove/add client
                   System.out.println("4 - Listar/adicionar/modificar/eliminar transporte");//modifie/remove/add transportation
                   System.out.println("5 - Terminar sessão");//end session
                   System.out.println("0 - Terminar programa");//end program
                   System.out.println("OP-> ");
                   op = ler.nextInt();

                    switch(op)
                    {
                        case 1:
                             System.out.println("1 - Voltar ao menu principal");//return to menu
                             System.out.println("OP-> ");
                             op = ler.nextInt();
                            break;


                        case 2:
                             System.out.println("1 - Listar produtos");
                             System.out.println("2 - Adicionar produtos");
                             System.out.println("3 - Modificar produtos");
                             System.out.println("4 - Eliminar produtos");
                             System.out.println("5 - Voltar");
                             System.out.println("OP-> ");
                             op = ler.nextInt();

                                 switch (op)
                                 {
                                     case 1:
                                         break;

                                     case 2:
                                         break;


                                     default: System.out.println("Opção inválida");    
                                 }
                           break;

                        case 3:
                             System.out.println("1 - Listar clientes");
                             System.out.println("2 - Adicionar clientes");
                             System.out.println("3 - Modificar clientes");
                             System.out.println("4 - Eliminar clientes");
                             System.out.println("5 - Voltar");
                             System.out.println("OP-> ");
                             op = ler.nextInt();

                                 switch (op)
                                    {
                                     case 1:
                                         break;

                                     case 2:
                                         break;

                                     case 3:
                                         break;

                                     case 4:
                                         break;

                                     case 5:
                                         break;

                                     default: System.out.println("Opção inválida");  
                                    }
                           break;

                        case 4:
                             System.out.println("1 - Listar transporte");
                             System.out.println("2 - Adicionar transporte");
                             System.out.println("3 - Modificar transporte");
                             System.out.println("4 - Eliminar transporte");
                             System.out.println("5 - Voltar");
                             System.out.println("OP-> ");
                             op = ler.nextInt();
                                 switch (op)
                                    {
                                     case 1:
                                         break;

                                     case 2:
                                         break;

                                     case 3:
                                         break;

                                     case 4:
                                         break;

                                     case 5:
                                         break;

                                     default: System.out.println("Opção inválida");  
                                    }
                           break; 

                        case 5:
                            break;

                        default: System.out.println("Opção inválida");   
                    }
                break; 

               default: System.out.println("Opção inválida");
           }

    }
    while(op!=0);
}

}

R Monkey
  • 131
  • 1
  • 10
  • 2
    You have a switch inside a switch inside a switch ? Please rethink your design. Even if you make it work now, it will most certainly break later. – SomeDude Jun 22 '17 at 02:20

3 Answers3

1

As per using labels in java without "loops"

You can use labels to define where you want to break to.

However, it's pretty unorthodox to do so outside of tight nested loops.

  Scanner ler = new Scanner(System.in);
  main:
  do {
    System.out.println("1 - login");
    System.out.println("0 - end program");
    System.out.print("OP-> ");
    op = ler.nextInt();

    switch(op) {
      case 1:
        menu:
        do {
          System.out.println("1 - visualise");
          System.out.println("2 - modify/remove/add product");
          System.out.println("3 - modify/remove/add client");
          System.out.println("4 - modify/remove/add transportation");
          System.out.println("5 - end session");
          System.out.println("0 - end program");
          System.out.println("OP-> ");
          op = ler.nextInt();

            switch(op)
            {
              case 1:
                do{
                System.out.println("1 - return to menu");
                System.out.println("OP-> ");
                op = ler.nextInt();
                } while(op!=1)
                break menu;

        //SNIP
         }
         while(true)
Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
  • not working, it still comes back to the System.out.println("1 - Iniciar sessão");//login System.out.println("0 - Terminar programa");//end program – R Monkey Jun 22 '17 at 02:36
  • you may need to adjust the labels to meet your usecase, I do not 100% understand the program flow you want. – Ryan Leach Jun 22 '17 at 02:37
1

Here is an example of how you can split your menus into methods and call them in the right locations. Note that each menu includes a loop. Also you had a problem with your op variables that were the same on all menus, which make them not work properly.

    static Scanner ler;
    public static void menuClientes(){
        int op= 0;

        while (op!= 5){
            System.out.println("1 - Listar clientes");
            System.out.println("2 - Adicionar clientes");
            System.out.println("3 - Modificar clientes");
            System.out.println("4 - Eliminar clientes");
            System.out.println("5 - Voltar");
            System.out.println("OP-> ");
            op = ler.nextInt();

            switch (op) {
            case 1:
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

            default:
                System.out.println("Opção inválida");
            }
        }
    }

    public static void menuTransporte(){
        int op= 0;

        while (op!= 5){
            System.out.println("1 - Listar transporte");
            System.out.println("2 - Adicionar transporte");
            System.out.println("3 - Modificar transporte");
            System.out.println("4 - Eliminar transporte");
            System.out.println("5 - Voltar");
            System.out.println("OP-> ");
            op = ler.nextInt();
            switch (op) {
            case 1:
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

            default:
                System.out.println("Opção inválida");
            }
        }
    }

    public static void menuPrincipal(){
        int op= 0;

        while (op!= 5){
            System.out.println("1 - Visualizar lista encomendas realizadas");// visualize
            System.out.println("2 - Listar/adicionar/modificar/eliminar produtos");
            System.out.println("3 - Listar/adicionar/modificar/eliminar clientes");
            System.out.println("4 - Listar/adicionar/modificar/eliminar transporte");
            System.out.println("5 - Terminar sessão");
            System.out.println("0 - Terminar programa");
            System.out.println("OP-> ");
            op = ler.nextInt();

            switch (op) {
            case 1:break;
            case 2:break;
            case 3: menuClientes();break;
            case 4: menuTransporte(); break;
            case 5: return;
            default:
                System.out.println("Opção inválida");
            }
        }
    }

    public static void main(String[] args) {

        int op;
        ler = new Scanner(System.in); //the declarion of this variable was put on the class itself to be accessible by all methods

        do {
            System.out.println("1 - Iniciar sessão");// login
            System.out.println("0 - Terminar programa");
            System.out.print("OP-> ");
            op = ler.nextInt();


            switch (op) {
            case 1: menuPrincipal();break;

            default:
                System.out.println("Opção inválida");
            }

        } while (op != 0);
    }
Isac
  • 1,834
  • 3
  • 17
  • 24
0

You would be better of separating each of those switch statements into their own functions / methods. This way you can give them appropriate names (i.e. make them easier to read what the intention of each switch option is). This reorganizing of your code will also allow you to better understand where the execution path will lead, when returning, instead of using break which you indicate exits from all the switches and back to the do-while loop.

Also following your example of 1, 1, 1 leads to a break which exits that switch to another break, which breaks to the third break which leaves you back in the do-while loop. So if you need to re-display the options of the second options you'll need another loop.

e.g. (to get you started. Also this is not tested and is only to give a rough idea of separating the switch options into functions.)

public static void main(String[] args) {
  int op;
  Scanner ler = new Scanner(System.in);
  do
  {
    System.out.println("1 - Iniciar sessão");//login
    System.out.println("0 - Terminar programa");//end program
    System.out.print("OP-> ");
    op = ler.nextInt();

    op = Login(op, ler);
  }
  while (op != 0);
}

public static int Login(int op, Scanner ler)
{
  switch (op)
  {
    case 0: // terminate program
      return 0;
    case 1:
      DisplayOptions(ler);
      return 1;
  }
}

public static int DisplayOptions(Scanner ler)
{
  do 
  {
    System.out.println("1: visualize");
    System.out.println("2: modify");
    ... etc...
    System.out.println("0: terminate program");

    int op = ler.nextInt();
    switch (op)
    {
      case 1:
        op = Visualize(ler);
        break;
      case 2:
        op = Modify(ler);
        break;
      case 0:
        System.out.println("Exiting program");
        break;
      default:
        System.out.println("invalid input");
    }
  }
  while (op != 0);
}

public static int Visualize(Scanner ler)
... etc ...
amisam
  • 91
  • 6