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

135
Vistas
How can I assign a certain case to execute the same code as another case in a java switch statement?

What I'm trying to do is allow a user to input different variations of a string(by that I mean, a user could enter either "Calculator", "Calc", "calculator", etc.), and still get the same code executed so I don't have to copy it into each case I want to do the same thing. i.e.

switch(input) {
    case "string1":
        statement 1;
        break;
    case "string2":
        statement 1;
        break;
    case "string3":
        statement 2;
        break;
    case "string4":
        statement 2;
        break;
    default:
        break;

Is there an easy way to do this, other than writing the same statement for each case?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A comment gave me the term I was looking for, so I searched it up and found the answer myself. I now know that I can combine cases by putting the case label with just the colons above the next case, that has the statement to be run in both cases. i.e.

switch(input) {
    case "string1":
    case "string2":
        statement1;
        break;
    case "string3":
    case "string4":
        statement2;
        break;
    default:
        break;

This gives my desired output. Thank you, Dave Newton!

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