Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
How can I not repeat the same case after I use it

So, I made a burger class with a method for extra stuff, my question is how can I use case 0,1,2 only 1 time, like if I use case 0, I can't use it anymore, I can use only 1 and 2, If I use case 1 after 0 , then I can use only case 2 since I used case 0 and 1 before , It's possible to do something like that ? If yes how ? The code:

         boolean flag=true;
         while(flag){
             System.out.println("Enter your choice for extra toppings ");
             int choice=scanner.nextInt();
             scanner.nextLine();
             switch(choice) {
                 case 0:
                     double salad = 0.35;
                     setAdditional(getAdditional() + salad);
                     System.out.println("salad added\n");
                     break;
                 case 1:
                     double bacon=1.05;
                     setAdditional(getAdditional()+bacon);
                     System.out.println("Bacon added \n");
                     break;
                 case 2:
                     double fries=0.79;
                     setAdditional(getAdditional()+fries);
                     System.out.println("fries added \n");
                     break;
                 case 3:
                     System.out.println("Done");
                     flag=false;

        }

        }
    } ```
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

  boolean flag = true;
    Set<Integer> set = new HashSet<>();
    while (flag) {
        System.out.println("Enter your choice for extra toppings ");
        int choice = scanner.nextInt();
        scanner.nextLine();
            switch (choice) {
                case 0:
                    if (!set.contains(choice)) {
                        double salad = 0.35;
                        setAdditional(getAdditional() + salad);
                        System.out.println("salad added\n");
                        break;
                    }
                    else {
                        System.out.println("Added already");
                    }
                    continue;
                case 1:
                    double bacon = 1.05;
                    setAdditional(getAdditional() + bacon);
                    System.out.println("Bacon added \n");
                    break;
                case 2:
                    double fries = 0.79;
                    setAdditional(getAdditional() + fries);
                    System.out.println("fries added \n");
                    break;
                case 3:
                    System.out.println("Done");
                    flag = false;

            }
            set.add(choice);
        }

    }

so I did it with Set in the end, only for salad, but it's the same for the rest, if someone else needs it.

over 4 years ago · Santiago Trujillo Denunciar

0

your cases go by integer numbers, so an array of boolean with 1 element for every option.

boolean[] allowed = new boolean[options]; (faster than hasMap of string to boolean).

add a check just before the "switch" statement:

if(allowed[choice] && choice != 3) {...}

you should also create an integer constant STOP_OPTION or something like that and use it in the above if-statement and in the final "case" of your switch statement. in your example, set it to 3. then later you can change it without replacing all instances of "3" in your code. but that's more of a styling suggestion.

the "flag" boolean is also redundant, the while loop can just check if choice != 3. be careful of NumberFormatExceptions!

good luck!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda