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

109
Visualizações
Switch case that dosent continue in a while loop
  int selection;  
  selection = 0;

  
  while (selection != -1)
  {
     selection = menu();

     switch (selection)
     {
     case 1:
        System.out.println("Hur många varor vill du lägga till?");
        int numberOfArticles = input();
        System.out.print("Du har lagt till:  " + numberOfArticles + " varor\n");
        articles = checkFull(articles, numberOfArticles);
        articleNumber = insertArticles(articles, articleNumber, numberOfArticles);
        break;
     case 2:
        System.out.println("Vilken artikel vill du ta bort?");
        removeArticle(articles);
        break;
     case 3:
        System.out.println("Visa artiklar");
        printArticles(articles);
        break;
     case 4:
        System.out.println("Vilken artikel vill du sälja? Hur stort antal vill du sälja?");
        sellArticle(sales, salesDate, articles);
        break;
     case 5:
        System.out.println("Orderhistorik");
        printSales(sales, salesDate);
        break;
     case 6:
        System.out.println("Avsluta");
        System.exit(0);
        break;
     default:
        System.out.print("Felaktig inmatning!\n");
        break;

When the user gives a string value then the programs says Wrong value then the program just stops. But when I select a number like 22 or something, the program says Wrong value and the loop continues and gives the user another chance to try. But when it comes to a string input it just stops and don't give the user another chance.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To end the loop selection should equal to -1. I think changing the default case like below will solve the problem.

default:
        System.out.print("Felaktig inmatning!\n");
        selection = -1;
        break;

Then it will exit the loop when the user gives an incorrect value.

over 4 years ago · Santiago Trujillo Relatório

0

public static int input()
   {

      int input;

      input = 0;

      while (input == 0)
      {
         if (keyboard.hasNextInt())
         {
            input = keyboard.nextInt();
            break;
         } else
         {
            input = -1;
            break;

         }

      }

      return input;
   }
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