2013-12-18 182 views
0

我爲教學目的編寫了一個簡單的程序,除了打印出您選擇的計算名稱和答案的部分之外,其他部分都可以使用。 if語句似乎執行了兩次,好像它在前進之前倒退了一步。打印出兩次

它將打印出「您是否想繼續」,但不是提示用戶輸入Y/N,而是打印出計算再次的答案,然後是否要繼續除外第二次它會實際等待輸入。

在此先感謝。

這裏是我的代碼:

import java.util.Scanner; 

public class Numbers 
{ 
    public static void main(String[] args) 
    { 
     Scanner reader = new Scanner(System.in); 

     boolean cont; 
     String name, yorn; 
     double num1, num2, answer; 
     int choice, iterator = 0; 

     System.out.print("What is your name? "); 
     name = reader.nextLine(); 

     System.out.print("Please enter a number: "); 
     num1 = reader.nextDouble(); 
     if(num1%2 != 0) 
     { 
      System.out.println(name + ", the number uou entered, " + num1 + ", is odd"); 
     } 
     else 
     { 
      System.out.println(name + ", the number uou entered, " + num1 + ", is even"); 
     } 

     System.out.print("Please enter a second number: "); 
     num2 = reader.nextDouble(); 
     if(num2%2 != 0) 
     { 
      System.out.println(name + ", the second number you entered, " + num2 + ", is odd"); 
     } 
     else 
     { 
      System.out.println(name + ", the second number you entered, " + num2 + ", is even"); 
     } 

     System.out.println("1. Add"); 
     System.out.println("2. Subtract"); 
     System.out.println("3. Multiply"); 
     System.out.println("4. Divide"); 
     System.out.print("Please enter the number for the operation you would like to perform on your numbers: "); 
     choice = reader.nextInt(); 

     cont = true; 
     while(cont) 
     { 

      while(choice != 1 && choice != 2 && choice != 3 && choice != 4) 
      { 
       System.out.print("The number entered is not one of the options. Please choose one of the operations: "); 
       choice = reader.nextInt(); 
      } 

      if(choice == 1) 
      { 
       answer = num1 + num2; 
       System.out.println(name +" the sum of " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else if(choice == 2) 
      { 
       answer = num1 - num2; 
       System.out.println(name +" the difference between " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else if(choice == 3) 
      { 
       answer = num1 * num2; 
       System.out.println(name +" the product of " + num1 + " and " + num2 + " is: " + answer); 
      } 
      else //if(choice == 4) 
      { 
       answer = num1/num2; 
       System.out.println(name +" the quotient of " + num1 + " and " + num2 + " is: " + answer); 
      } 


      System.out.print("Would you like to do anything else (Y/N)? "); 
      yorn = reader.nextLine(); 

      if(yorn.equals("Y")) 
      { 
       System.out.println("1. Add"); 
       System.out.println("2. Subtract"); 
       System.out.println("3. Multiply"); 
       System.out.println("4. Divide"); 
       System.out.print("Please enter the number for the operation you would like to perform on your numbers: "); 
       choice = reader.nextInt(); 

      } 
      else if (yorn.equals("N")) 
      { 
       System.out.println("Thank you for using this prgram. Have a good day"); 
       cont = false; 

      } 
     } 
    } 
} 
+1

其中'if' PLZ具體 – exexzian

+1

如果哪個塊哪些while循環?你看到兩行文字? – clay

+0

檢查編輯。我很抱歉不夠清楚。感謝您的回覆 – PracticeFirst

回答

3

的問題是nextDouble()功能。是的,這會讀取一個double,但它也會將新行字符(\n)讀入緩衝區。因此,下次您撥打nextLine()時,會立即解析換行符,而不是等待輸入。在再次詢問您的輸入之前,只需調用另一個reader.nextLine()即可清除換行符的緩衝區。

+0

修復它。非常感謝。 :) – PracticeFirst

0

As @ adback03表示掃描儀(obj)。 nextDouble()緩衝區「\ n」和nextLine()它從緩衝區中讀取;因此while循環繼續

[關閉主題]我suggesst你使用的switch-case

switch(choice) { 
    case 1: System.out.println(num1 + num2); break; 
    case 2: System.out.println(num1 - num2); break; 
    case 3: System.out.println(num1 * num2); break; 
    case 4: System.out.println(num1/num2); break; 
    default: System.out.println("Invalid choice"); 
} 

更容易閱讀:)