2017-10-06 23 views
-3
import java.util.*; 
import java.math.*; 

public class Arithmetic 
{ 

public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    double mealCost = scan.nextDouble(); // original meal price 
    double tipPercent = scan.nextDouble(); // tip percentage 
    double taxPercent = scan.nextDouble(); // tax percentage 
    scan.close(); 

    // Write your calculation code here. 
    tipPercent = mealCost*tipPercent/100.0; 
    taxPercent =mealCost*taxPercent/100.0; 



    //cast the result of the rounding operation to an int and save it as totalCost 

    double totalCost = mealCost + tipPercent + taxPercent; 

    // Print your result 
    int total = (int)totalCost; 
    System.out.println("The total meal cost is " + total + " dollars."); 
} 
} 

輸入:
20.75
預期輸出:總用餐的費用爲23美元。未能獲得預期的輸出hackerrank DAY2程序

結果輸出:總餐費爲26美元。

這個程序有什麼問題?

+0

你調試你的程序了嗎?你是否確認了單個步驟產生了正確的結果?你爲什麼使用數值''20''和''8''? – f1sh

+3

你沒有使用你的輸入參數,而是用硬編碼的數字覆蓋它們。 –

+0

你的預期結果不應該是33美元嗎?只需簡單地加上所有三個就可以得到33.75! – procrastinator

回答

0

您不使用小費和稅收輸入。

它應該是:

tipPercent = mealCost*tipPercent/100.0; 
taxPercent = mealCost*taxPercent/100.0; 
+0

是的,我做到了。即使它的結果與預期相同。 –

+0

我無法重現您的問題。當我拿起你的代碼並對它進行修改時,它輸出了預期的結果。也許你的計算出了問題。 –

0

最後這段代碼工作完全

import java.util.*; 
import java.math.*; 

public class Arithmetic 
{ 

public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    double mealCost = scan.nextDouble(); // original meal price 
    double tipPercent = scan.nextDouble(); // tip percentage 
    double taxPercent = scan.nextDouble(); // tax percentage 
    scan.close(); 

    // Write your calculation code here. 
    tipPercent = mealCost*tipPercent/100.0; 
    taxPercent =mealCost*taxPercent/100.0; 

    // cast the result of the rounding operation to an int and save it as 
//totalCost 
    double totalCost = mealCost + tipPercent + taxPercent; 

    if((tipPercent + 0.5)>=((int)tipPercent + 1)) 
     { 
      totalCost += 1; 
     } 
     else 
    { 
    } 
    // Print your result 
    int total = (int)totalCost; 
    System.out.println("The total meal cost is " + total + " dollars."); 
} 
} 
+0

這不是一個通用的解決方案。你只是硬編碼設置「totalCost」的值。 – procrastinator