我已经完成了这项作业,我将要扔掉这台笔记本电脑。下面的代码运行,但是当我在MindTap中对其进行测试时,我得到了最下面的消息。我不知道我在做什么错,或者为什么它说错了。
任务: 为照相簿商店编写三种重载的computeBill方法:
computeBill收到一个参数时,它表示订购的一本相册的价格。加上8%的税,并返还应付总额。 当computeBill收到两个参数时,它们代表一本相簿的价格和订购的数量。将两个值相乘,加上8%的税,然后返回应付总额。 当calculateBill收到三个参数时,它们代表一本相簿的价格,订购的数量和优惠券价值。将数量和价格相乘,将结果乘以票面价值,然后加8%的税金,并返还应付总额。
我的编码:在此处输入代码 公共类帐单{
public static void main(String args[]){
double yourTotal;
yourTotal = computeBill(31.00);
displayTotal (yourTotal);
yourTotal = computeBill (31, 2);
displayTotal(yourTotal);
yourTotal = computeBill(31, 2, .2);
displayTotal (yourTotal);
}
public static double computeBill (double price)
{double total = price * 1.08;
System.out.println ("You ordered 1 photobook for $" + price);
System.out.println("Plus sales tax 8%");
return total;}
public static double computeBill (double price, int qty) {
double subtotal = price * qty;
double total = subtotal * 1.08;
System.out.println ("You ordered" + qty + " photobook(s) for $" + price);
System.out.println("Subtotal =" + subtotal);
System.out.println("Plus sales tax 8%");
return total;
}
public static double computeBill (double price, int qty, double discount) {
double subtotal = price * qty;
subtotal = subtotal - (subtotal * discount);
double total = subtotal * 1.08;
System.out.println ("You ordered " + qty + " photobook(s) for $" + price);
System.out.println("Subtotal = " + subtotal);
System.out.println("Less your " + (discount * 100) + "% discount");
System.out.println("Plus sales tax 8%");
return total;
}
public static void displayTotal (double total){
System.out.println("Total: $" + total);
}
}
结果当我测试时,MindTap给了我: 建立状态 建立成功 测试输出 您以$ 31.0的价格订购了2本相册 小计= 62.0 加上营业税8% [FAILED]:unitTest(CodevolveTest12f618f0):空 假 测试内容 Billing tester30 = new Billing();
@Test
public void unitTest() {
assertTrue(tester30.computeBill(31, 2) == 66.96);
}
请帮助我。我被卡住了!!!
答案 0 :(得分:0)
您需要限制小数点后的位数,以使比较变得合乎逻辑:
public static double computeBill (double price, int qty) {
double subtotal = price * qty;
double total = subtotal * 1.08;
System.out.println ("You ordered" + qty + " photobook(s) for $" + price);
System.out.println("Subtotal =" + subtotal);
System.out.println("Plus sales tax 8%");
// need to limit to two digit after decimal
return Double.parseDouble(new DecimalFormat("##.##").format(total));
}
因此,您进行测试:
@Test
public void unitTest() {
assertTrue(tester30.computeBill(31, 2) == 66.96); // comparing two digits after decimal
}