2012-07-27 99 views
0

我是一個初學者,我在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處理在年底投入fnumsnum

,我增加了else語句。它應該通知用戶何時他/她沒有輸入有效的method進行操作。

問題是,每當我執行這個代碼,不管是什麼我輸入作爲method將首先很好地工作,索要fnumsnum,然後輸出的答案,但隨後,它也執行的代碼塊在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: 
+3

此問題已被詢問太多次。看看[在nextInt後使用nextLine時出現掃描程序問題](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint)。 – 2012-07-27 10:27:20

回答

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."); 
     } 
    } 
} 
} 
+0

謝謝!但請你告訴我爲什麼會發生這種情況?如果我知道它的原因,我可以很容易地記住它。 – 2012-07-27 10:39:34

+1

@用戶每當我們輸入一個數字並按回車,scan.nextInt()只讀取數字而不是「行尾」。當我們做scan.nextLine();它從第一個輸入中讀取仍在緩衝區中的「行尾(\ n)」。 – 2012-07-27 10:40:51

+0

好!萬分感謝!這個問題實際上在吃我的大腦。 – 2012-07-27 10:42:45

1

String method = scan.nextLine();返回""計算後。由於空白字符串在任何if語句中都不匹配else將被評估

+0

但爲什麼會發生?我們沒有把它的值存儲在一個變量中嗎?它不應該爲空,而是在下一個循環中修改它。 – 2012-07-27 10:40:33

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 
    }