Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Necesito ayuda para restablecer una declaración de cambio

Soy nuevo en Java y estaba haciendo una calculadora simple, pero quiero que el código vuelva al principio si no se ingresa una de las opciones (agregar, sub, mul o div). Si la forma correcta es una declaración de bucle, ¿cómo haría para cambiar mi código para eso?

 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 answers
Answer question

0

Puede usar do{}while(<condition>) por ejemplo:

 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 Report

0

Puede envolverlo en do while loop y break si solo la entrada es válida, por ejemplo:

 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 Report

0

Por lo que puedo entender, desea repetir su caso hasta que el usuario proporcione "sum, div, sub, mul". Puede hacerlo con 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!