努力获取正确的方法返回值

时间:2019-10-20 05:28:42

标签: java

我对这种方法的目标(这是更大任务的一部分)是获取2种配方的糖袋总数。我相信我所有的转换都是正确的,但是当按照我的教授为作业做的测试程序检查它时,我没有得到正确的输出。我尝试为双精度数据类型在小数和小数之间进行更改,但这确实会稍微改变我的结果。我不确定我缺少什么。 (每磅杯子的转换由教授提供)

    public static int bagsOfSugar(int cookieCount, int loafCount) {
        double sugarPerCookies = 3/2;   // Measured in cups
        double cookiesInRecipe = 48.0;
        double sugarPerLoaf = 3/4;      // Measured in cups
        double sugarPerBag = 1.0;       // Measured in lb
        double cupsPerPound = 2.0;      // There are 2 cups of sugar per pound

        double sugarForBread = (sugarPerLoaf / 1.0) * (sugarPerBag / cupsPerPound) * (1.0 / sugarPerBag); // Conversion from cups to bags of sugar for bread
        double sugarForCookies = (sugarPerCookies / cookiesInRecipe) * (sugarPerBag / cupsPerPound); // Conversion from cups to bags of sugar for cookies
        //Returns how many bags of sugar to purchase
        return (int)(Math.ceil(cookieCount * sugarForCookies) + (loafCount * sugarPerLoaf)); 
}

我对测试程序的反馈是:

Incorrect bagsOfSugar calculation! Should be 5, got 7

0 个答案:

没有答案