2014-09-27 133 views
0

這是代碼。基本上,在我輸入第二個客戶賬單金額後,它會過早地讀我一個提示,並詢問是否有另一個客戶。我希望它能問我是否有另一位顧客,但我不希望它詢問有關提示,直到我輸入「n」。有什麼想法嗎?小費計算器聲明混淆

import java.util.Scanner; 


public class H3_TipCalc { 

    public static void main(String[] args) { 

    Scanner input = new Scanner (System.in); 
    System.out.println("Please enter your bill amount."); 
    double bill = input.nextDouble(); 

    String multiplecust = ("Y"); 
    //int mc=1; 

    while (multiplecust.equalsIgnoreCase("y")) 

    { 


      Scanner usrin = new Scanner (System.in); 

     System.out.println("Is there another customer? y or n?"); 
     multiplecust = usrin.nextLine(); 
     //mc++; 



     if (multiplecust.equals("y")){ 
      System.out.println("Please enter their bill amount."); 

      } 
     else if (multiplecust.equals("n")){ 
      System.out.println("What tip prcentage would you like to use? Enter as a decimal."); 
     } 

     double tip = usrin.nextDouble(); 
     System.out.println("Your tip owed will be " + bill*tip + "."); 
    } 
    } 

} 

回答

0

因爲else if的,你可以輸入最後一位客戶的賬單金額或提示百分比,但不是兩者。相反,嘗試這樣的邏輯:

while(there are more individual customer amounts to enter) { 
    enter next customer amount 
} 

get tip percentage 

show final bill 

注意尖端百分比while循環後進入的,因爲它只是一個一次性的項目。

0

你的邏輯扭曲了。

更好的移動整個邏輯循環內,並且在它的端部(未開始),檢查出:

do 
    read bill amount 
    read tip amount 
    read next customer 
while (wants to continue)