我是一個初學者,我在while循環嵌套if語句試驗。我用這種方式製作了一個基本的計算器,但是我遇到了一些小問題。我會先分享代碼,然後提出我的問題。我有問題與Java「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:
此問題已被詢問太多次。看看[在nextInt後使用nextLine時出現掃描程序問題](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint)。 – 2012-07-27 10:27:20