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
}
}
}
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);
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);
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);