2014-03-03 25 views
-1

下面是我爲我正在採取的類的代碼...我需要知道如何正確地編寫此產品的總成本,我不能爲我的生活弄清楚如何正確地做到這一點。每個產品的價格是1美元,我還需要計算7%的銷售稅。幫幫我!!!計算totalCost

import javax.swing.JOptionPane;

/** * @author巴里 */ 公共類CocaCola1 {

public static void main(String[] args) 
    { 


     String openingMsg = "*** Welcome to CocaCola Online Ordering System ***\n" 
        + "Do yourself a favor and order some Cola... NOW!"; 
     JOptionPane.showMessageDialog(null, openingMsg); 
     System.out.println(0); 

     String name = getUserInput("What is your name?"); 
     System.out.println(name); 

     String returnCustomer = getUserInput("Are you a returning customer? (yes/no)"); 
     System.out.println(returnCustomer); 

     String orderType = getUserInput("Would you like to purchase Dasani, Coke, Diet Coke, or Cherry Coke?"); 
     System.out.println(orderType); 

     String orderAmount = getUserInput("How many units would you like to purchase?"); 
     System.out.println(orderAmount); 
     Integer.parseInt(orderAmount); 
     // each unit costs $1.00 so orderAmount is equal to cost per unit 

     double salesTax = 0.7; 


     // create and display output string 
       String outputMsg 
       = "Hello " + name + ".\n\n" + 
        "Your return customer status is " + returnCustomer + ".\n" + 
        "You ordered " + orderAmount + 
        " units of " + orderType + ".\n" + 
        "Your total cost is $" + orderAmount + salesTax + ".\n" + 
        "Thank you for visiting CocaCola" + ".\n\n" + 
        "Your order will be proccessed ASAP.\n"; 
       JOptionPane.showMessageDialog(null, outputMsg); 
    } 



    private static String getUserInput(String prompt) 
    { 
     int failCount = 0; 
     do 
     { 
      String answer = JOptionPane.showInputDialog(prompt); 
      if (answer == null) 
      { 
       System.exit(0); 
      } 

      answer = answer.trim(); 
      if (answer.isEmpty()) 
      { 
       JOptionPane.showMessageDialog(null, "You must provide a non-blank answer"); 
       failCount = failCount + 1; 
      } else 
      { 
       return answer; 
      } 
     } while (failCount < 3); 

     JOptionPane.showMessageDialog(null, "You failed three times to provide input... Try again later!"); 
     System.exit(0); 
     return null; // means nothing, as I just exited on the prior line, but need it to compile 


} // end main() 

} //結束類CocaCola1

+0

您有什麼問題?不要只是在這裏轉儲你的代碼並說它不起作用 – Ghost

+0

如果你開始閱讀,我確實問過一個問題。我試圖正確計算totalCost,但我不確定用於執行此計算的正確代碼。 – user3375536

+0

so'totalCost = Quantity * PriceEach *(1 + SalesTaxRate)'? – Ghost

回答

-1

orderAmount +銷售稅只會是如果銷售稅是靜態的。您需要通過銷售稅乘以orderAmount並添加銷售稅

例如:orderAmount *銷售稅+ prderAmount

或設置銷售稅到1.7,這將是 orderAmount *銷售稅