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

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

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

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

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 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