税收取决于他们的工资差距有多大。如果薪水在对方10000美元以内,则税额为5%。如果他们相差超过1万美元,那么这笔税是5%的较大收入加上3%的较小收入。 有人可以解释我做错了我的if语句是不是让测试通过。有人可以解释我的代码有什么问题吗?
public double spouseDistance(double income1, double income2)
{
double tax;
double totalincome = income1 + income2;
double incomedistance = income1 - income2;
if (incomedistance > 10000)
{
if(income1 > income2)
{
tax = income1 * 0.05 + income2 * 0.03;
}else
{
tax = income2 * 0.05 + income1 * 0.03;
}
}
else
{
tax = totalincome * 0.05;
}
return tax;
}
}
@Test
public void testSpousesDistance()
{
TaxCalculator tc = new TaxCalculator();
// The border case here is when the difference between the
// salaries is 10,000 and the value changes at 10,000.01.
// Since the order of the parameters shouldn't matter, do
// both cases with both parameter orders
assertEquals(40000*0.05, tc.spouseDistance(25000,15000), 0.001);
assertEquals(40000*0.05, tc.spouseDistance(15000,25000), 0.001);
assertEquals(30000.01*0.05 + 20000*0.03,tc.spouseDistance(20000.00,30000.01), 0.001);
assertEquals(30000.01*0.05 + 20000*0.03,tc.spouseDistance(30000.01, 20000.00), 0.001);
}
}
为什么不真正运行代码并查看返回的“税”是什么? – jack3694078
对于下一次遇到这样的问题时的建议:尝试使用调试器运行测试,并检查它是否按照您的预期一步一步地运行。 – Volune