我是一个初学者,我正在尝试在 while 循环中嵌套 if 语句。我用这种方法做了一个基本的计算器,但我有一个小问题。我将首先分享代码,然后提出我的问题。
这是代码:
import java.util.Scanner;
class whileloop{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("This is a basic calculator.");
System.out.println("To exit, input exit when asked to input the method.");
System.out.println("----------------------------------------------");
while (true){
System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
System.out.println("----------------------------------------------------");
System.out.print("Please enter a method here:");
String method = scan.nextLine();
if (method.equals("exit")){
System.out.println("You chose to exit.");
break;
}
else if (method.equals("+")){
System.out.println("You chose to add.");
System.out.print("Please enter first number here:");
double fnum = scan.nextInt();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
double ans = fnum + snum;
System.out.print("The answer is");
System.out.println(ans);
}
else if(method.equals("-")){
System.out.println("You chose to subtract.");
System.out.print("Please enter first number here:");
double fnum = scan.nextInt();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
double ans = fnum - snum;
System.out.print("The answer is");
System.out.println(ans);
}
else if(method.equals("*")){
System.out.println("You chose to multiply.");
System.out.print("Please enter first number here:");
double fnum = scan.nextInt();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
double ans = fnum * snum;
System.out.print("The answer is");
System.out.println(ans);
}
else if(method.equals("/")){
System.out.println("You chose to divide.");
System.out.print("Please enter first number here:");
double fnum = scan.nextInt();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
double ans = fnum / snum;
System.out.print("The answer is");
System.out.println(ans);
}
else{
System.out.println("Invalid input. please select a valid input from the list of operators given.");
}
}
}
}
如您所见,它要求一个变量method
来处理输入fnum
和snum
最后,我添加了 else 语句。当他/她没有输入有效的操作时,它应该通知用户method
。
问题是,每当我执行此代码时,无论我输入什么,method
它都会首先完美运行,询问fnum
然后snum
输出答案,但随后,它也会执行 else 语句中的代码块。
如何防止这种情况?请帮忙!
此外,这是我得到的那种输出:(我把它放在一个代码块中只是为了让它看起来更有条理。)
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:+
You chose to add.
Please enter first number here:10
Please enter second number here:10
The answer is20.0
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here:Invalid input. please select a valid input from the list of operators given.
to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'
----------------------------------------------------
Please enter a method here: