我是初学者,我正在尝试在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:
答案 0 :(得分:2)
use scan.nextLine() immediately after scan.nextInt().
每当我们输入一个数字并按Enter键时,scan.nextInt()只读取不是“行尾”的数字。 当我们做scan.nextLine();它读取“行尾(\ n)”,它仍然在第一个输入的缓冲区中。
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();
scan.nextLine();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
scan.nextLine();
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();
scan.nextLine();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
scan.nextLine();
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();
scan.nextLine();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
scan.nextLine();
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();
scan.nextLine();
System.out.print("Please enter second number here:");
double snum = scan.nextInt();
scan.nextLine();
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.");
}
}
}
}
答案 1 :(得分:1)
String method = scan.nextLine();
会返回""
。由于空格字符串在任何if语句中都不匹配else
将被评估
答案 2 :(得分:0)
这是因为scan.nextLine()
在计算后返回""
。所以你可以添加这一行。
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();
// add this line
if (method.equals(""))
{
continue; // no "" will be there
}