我正在尝试制作一个计算器。这些运算符:x
、+
、-
、/
工作正常。
但我希望用户在得到数学问题的答案后能够做两件事。
询问用户是否要继续。
- 如果用户输入
yes
,他就可以输入 2 个数字,以便再次计数。 - 如果用户输入
no
,则直接关闭。
这是我的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Minscanner = new Scanner(System.in);
int nr1 = Integer.parseInt(Minscanner.nextLine());
int nr2 = Integer.parseInt(Minscanner.nextLine());
int yes = Integer.parseInt(Minscanner.nextLine());//trying to fix reset
int ans =0;
int reset = J;/trying to make it reset if user types in yes
String anvin = Minscanner.nextLine();
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
System.out.println(ans);
}
if(anvin.equalsIgnoreCase("yes")) {
return;
}
}
}
最佳答案
将您的代码放入
do {
...
} while (condition);
循环,在您的情况下,如果用户说"is",则条件将类似于wantToContinue
。
那么程序将不会结束,除非用户不再想计算。
关于Java:请求继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717746/