Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌套if语句有问题_Java_Nested - Fatal编程技术网

Java 嵌套if语句有问题

Java 嵌套if语句有问题,java,nested,Java,Nested,理论上,不同层次的收入按百分比计税是不同的。前50k的10%,下50k的15%,超过100k的25% public double getTaxesWithheld() { if (taxableIncome >= 100000.0) { taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0)); taxableIncome = taxableIncome - 100000.0

理论上,不同层次的收入按百分比计税是不同的。前50k的10%,下50k的15%,超过100k的25%

 public double getTaxesWithheld() {

    if (taxableIncome >= 100000.0) {

        taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
        taxableIncome = taxableIncome - 100000.0;
    } else {
        if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
            taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
            taxableIncome = taxableIncome - 50000.0;
        } else {
            if (taxableIncome < 50000.0) {
                taxesWitheld = taxesWitheld + (.1 * (taxableIncome - 25000.0));
            }
        }

        if (taxableIncome <= 0) {
            return 0.0;
        }

    }
    return taxesWitheld;
}

不会处理TaxesWithHold()、8600.15、0.005

以下是一种可能产生更好结果的完全不同的方法:

 public double getTaxesWithheld() {

    over100k = Math.max(taxableIncome-100000, 0);
    taxableIncome -= over100k;
    over50k = Math.max(taxableIncome-50000, 0);
    taxableIncome -= over50k;

    taxesWitheld = taxesWitheld + (.25 * over100k);
    taxesWitheld = taxesWitheld + (.15 * over50k);
    taxesWitheld = taxesWitheld + (.1 * taxableIncome);
    return taxesWitheld;
}

这里有一个完全不同的方法,可能会产生更好的结果:

 public double getTaxesWithheld() {

    over100k = Math.max(taxableIncome-100000, 0);
    taxableIncome -= over100k;
    over50k = Math.max(taxableIncome-50000, 0);
    taxableIncome -= over50k;

    taxesWitheld = taxesWitheld + (.25 * over100k);
    taxesWitheld = taxesWitheld + (.15 * over50k);
    taxesWitheld = taxesWitheld + (.1 * taxableIncome);
    return taxesWitheld;
}

不应该是if-else语句,因为减去10万后,剩下的部分仍需纳税

if (taxableIncome >= 100000.0) {
    taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
    taxableIncome = taxableIncome - 100000.0;
}

if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
        taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
        taxableIncome = taxableIncome - 50000.0;
}
if(应纳税所得额>=100000.0){
纳税所得额=纳税所得额+(.25*(应纳税所得额-100000.0));
应纳税所得额=应纳税所得额-100000.0;
}

if(taxableIncome>=50000.0&&taxableIncome不应该是if-else语句,因为在减去10万之后,其余部分仍需纳税

if (taxableIncome >= 100000.0) {
    taxesWitheld = taxesWitheld+ (.25 * (taxableIncome - 100000.0));
    taxableIncome = taxableIncome - 100000.0;
}

if (taxableIncome >= 50000.0 && taxableIncome <= 100000.0) {
        taxesWitheld = taxesWitheld + (.15 * (taxableIncome - 50000.0));
        taxableIncome = taxableIncome - 50000.0;
}
if(应纳税所得额>=100000.0){
纳税所得额=纳税所得额+(.25*(应纳税所得额-100000.0));
应纳税所得额=应纳税所得额-100000.0;
}

如果(taxableIncome>=50000.0&&taxableIncome你的巢有点不稳定

if (taxableIncome >= 100000.0) {
    ...
} else if (taxableIncome >= 50000.0) { 
// you don't need if(<= 100000), it's implied since you already 
// know it's !(taxableIncome >= 1000000) from the first if statement
    ...
} else if (taxableIncome > 0) {
    ...
} else return 0; 
// again, you don't need if(<= 0) here, 
// it's implied since you know that !(taxableIncome > 0)
if(应纳税所得额>=100000.0){
...
}如果(应纳税所得额>=50000.0){
//您不需要从第一个if语句中选择if(=1000000)
...
}其他条件(应纳税所得额>0){
...
}否则返回0;

//同样,如果你的巢有点不稳定,你也不需要

if (taxableIncome >= 100000.0) {
    ...
} else if (taxableIncome >= 50000.0) { 
// you don't need if(<= 100000), it's implied since you already 
// know it's !(taxableIncome >= 1000000) from the first if statement
    ...
} else if (taxableIncome > 0) {
    ...
} else return 0; 
// again, you don't need if(<= 0) here, 
// it's implied since you know that !(taxableIncome > 0)
if(应纳税所得额>=100000.0){
...
}如果(应纳税所得额>=50000.0){
//您不需要从第一个if语句中选择if(=1000000)
...
}其他条件(应纳税所得额>0){
...
}否则返回0;

//再说一次,你不需要(到底有什么问题?JUnit测试用例对于年薪超过10万英镑且介于50万-100万英镑之间的员工无法正常运行提供您的测试用例。我的直觉是,您应该以某种方式对每一笔资金征税。到底有什么问题?JUnit测试用例对于年薪超过10万英镑且介于50万-100万英镑之间的员工无法正常运行。)提供您的测试用例。我的直觉是,您应该以某种方式对每一部分资金征税。请记住,这种方法确实存在浮点精度问题的热点。谢谢,我对如何针对不同收入水平正确实施该声明感到困惑。请记住,这种方法确实存在浮点精度问题的热点谢谢,我对如何针对不同的收入水平恰当地执行报表感到困惑