Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda