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

191
Vistas
Need helping on resetting a switch statement

I'm new to java and i was making a simple calculator but I want the code to go back to the beginning if one of the options(add, sub, mul or div) are not inputed. If the correct way is a loop statement how would i go about changing my code for that?

import java.util.Scanner;
public class Calc {

    public static void main(String[] args) {

    Scanner math = new Scanner(System.in);
     double fnum, snum, answer;
     System.out.println("Enter add for addition");
     System.out.println("Enter sub for subtraction");
     System.out.println("Enter mul for multiplication");
     System.out.println("Enter div for division");
        String txt = math.nextLine();
        switch (txt) {
        case "add":
        // Code for addition
            break;
        case "sub":
            //code for subtraction
            break;
        case "mul":
        // Code for multiplication.
           break;
        case "div":
            //just code for division
            break;
        default:
            System.err.println("Please enter a valid option");
        // How do i make it go back to the beginning of the switch
    }
    }   
}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use do{}while(<condition>) for example :

boolean check = false;
do {
    String txt = math.nextLine();
    switch (txt) {
        case "add":
            System.out.println("You selected ADDITION");
            System.out.println("Enter first number");
            fnum = math.nextDouble();
            System.out.println("Enter second number");
            snum = math.nextDouble();
            answer = fnum + snum;
            System.out.println(answer);
            check = true;//change your boolean to true
            break;
        case "sub":
            System.out.println("You selected SUBTRACTION");
            System.out.println("Enter first number");
            fnum = math.nextDouble();
            System.out.println("Enter second number");
            snum = math.nextDouble();
            answer = fnum - snum;
            System.out.println(answer);
            check = true;//change your boolean to true
            break;
        case "mul":
            System.out.println("You selected MULTIPLICATION");
            System.out.println("Enter first number");
            fnum = math.nextDouble();
            System.out.println("Enter second number");
            snum = math.nextDouble();
            answer = fnum * snum;
            System.out.println(answer);
            check = true;//change your boolean to true
            break;
        case "div":
            System.out.println("You selected DIVISION");
            System.out.println("Enter first number");
            fnum = math.nextDouble();
            System.out.println("Enter second number");
            snum = math.nextDouble();
            answer = fnum / snum;
            System.out.println(answer);
            check = true;//change your boolean to true
            break;
        default:
            System.err.println("Please enter a valid option");
    }
} while (!check);
over 4 years ago · Santiago Trujillo Denunciar

0

You can wrap it into do while loop and break out if only the input is valid, e.g.:

boolean valid = true;
do{
    String txt = math.nextLine();
    switch (txt) {
    case "add":
        System.out.println("You selected ADDITION");
        System.out.println("Enter first number");
        fnum = math.nextDouble();
        System.out.println("Enter second number");
        snum = math.nextDouble();
        answer = fnum + snum;
        System.out.println(answer);
        break;
    case "sub":
        System.out.println("You selected SUBTRACTION");
        System.out.println("Enter first number");
        fnum = math.nextDouble();
        System.out.println("Enter second number");
        snum = math.nextDouble();
        answer = fnum - snum;
        System.out.println(answer);
        break;
    case "mul":
        System.out.println("You selected MULTIPLICATION");
        System.out.println("Enter first number");
        fnum = math.nextDouble();
        System.out.println("Enter second number");
        snum = math.nextDouble();
        answer = fnum * snum;
        System.out.println(answer);
        break;
    case "div":
        System.out.println("You selected DIVISION");
        System.out.println("Enter first number");
        fnum = math.nextDouble();
        System.out.println("Enter second number");
        snum = math.nextDouble();
        answer = fnum / snum;
        System.out.println(answer);
        break;
    default:
        System.err.println("Please enter a valid option");
        valid = false;
    }
}while(!valid);
over 4 years ago · Santiago Trujillo Denunciar

0

As far as i can understand you want to loop your case until the user gives "sum, div,sub,mul" You can do it with do{}while();.

    do{

        Input of user

        Case 1 answer=true;

        Case 2 answer=true;

        Default answer=false;

    }while(!answer);
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