2016-03-04 121 views
0

税收取决于他们的工资差距有多大。如果薪水在对方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); 
} 

}

+0

为什么不真正运行代码并查看返回的“税”是什么? – jack3694078

+0

对于下一次遇到这样的问题时的建议:尝试使用调试器运行测试,并检查它是否按照您的预期一步一步地运行。 – Volune

回答

3

你要设置incomeDistanceMath.abs(income1 - income2)得到的差值的绝对值。你的方式,如果income2>income1你分支到错误的代码块。

以您的20,000和30,000.01为例。此时incomeDistance变成20,000-30,000.01或-10,000,其中小于10,000。

1

您必须确保收入距离的结果不是负值。因为如果收入2大于收入1,则在您的代码中,结果将显示为负值。

如果结果是否定的,乘以结果(-1),如果你不 要包括整个图书馆只是一个单一的目的。

相关问题