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

288
Vistas
Java: Switch inside do while loop

I tried to make a multiple random pick ups that would print the message for each input number, i.e. serie of numbers (4 2 17 0) where 0 will stop the code. Got wrong output


public class Main {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       int number=scanner.nextInt();
       
  int x = number;
do{switch(x) {

case 1:
      System.out.println("Language selection");
break;
case 2:
       System.out.println("Customer support");
break;
case 3:
       System.out.println("Check the balance");
break;

case 4:
       System.out.println("Check loan balance");
break;
case 0:
System.out.println("Exit");
break;
  
default:
return;

}
if (x==0)
{break;}
x++;
}

while (true);

   }
} ```
![enter image description here](https://i.stack.imgur.com/RJBYn.jpg)

![enter image description here](https://i.stack.imgur.com/wCm4p.jpg)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use Scanner#hasNextInt to terminate the loop when integers are exhausted.

Demo:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextInt()) {
            int x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            case 0:
                System.exit(0);
            }
        }
    }
}

A sample run:

4 2 17 0
Check loan balance
Customer support

Also, note that you need break to avoid the cases falling through inadvertently. I did not see any use of default in this case and therefore, I have removed it from the demo code.

Alternatively, using a do-while loop:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = 0;

        do {
            x = scanner.nextInt();
            switch (x) {
            case 1:
                System.out.println("Language selection");
                break;
            case 2:
                System.out.println("Customer support");
                break;
            case 3:
                System.out.println("Check the balance");
                break;
            case 4:
                System.out.println("Check loan balance");
                break;
            }
        } while (x != 0);
    }
}
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