我是java的新手,我正在制作一个简单的计算器,但如果其中一个选项(add,sub,mul或div)没有被输入,我希望代码能够回到起点。如果正确的方法是循环语句,我将如何改变我的代码?
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
}
}
}
答案 0 :(得分:0)
您可以使用do{}while(<condition>)
例如:
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);
答案 1 :(得分:0)
如果输入有效,您可以将其包装到do while
循环和break
中,例如:
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);
答案 2 :(得分:0)
据我所知,你想循环你的情况,直到用户给出&#34; sum,div,sub,mul&#34; 你可以用do {} while();。
来做 do{
Input of user
Case 1 answer=true;
Case 2 answer=true;
Default answer=false;
}while(!answer);
答案 3 :(得分:0)
图案:
boolean condition = true;
while(condition){
//Input of user
switch(input){
Case 1:
//somecode
break;
Case 1:
//somecode
break;
case "exit":
condition = false;
break;
default:
//wrong input
break;
}
}
工作代码:
package com.example;
import java.util.Scanner;
public class MyClass {
public static void main(String... args) {
Scanner math = new Scanner(System.in);
double fnum, snum, answer;
boolean exit = true;
while (exit) {
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");
System.out.println("Enter ext for exit");
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;
case "ext":
exit = false;
break;
default:
System.err.println("Please enter a valid option");
}
}
}
}