java 中是否有执行 if-else 语句之外的代码?

Is there anyway to execute codes outside the if-else statement in java?

*已编辑 java中if-else语句之外的代码有什么办法可以执行吗? 使 if-else 语句将获取 if 语句之前的代码和金额,并执行它。 像这样:

double tax = 0;
    
if (status ==0) {
        int taxincome0 = 8350;
        int taxincome1 = 33950;
        double taxrate0 = (taxincome0 * 0.10);
        double taxrate1 = ((taxincome1 - taxincome0) * 0.15);
        
        if (income <= taxincome0){
            tax = income * 0.10;
        }
        
        else if (income <= taxincome1){
            tax = taxrate0 + (income - 8350) *0.15;
        }
    }

else if (status ==1) {
            int taxincome0 = 16700;
            int taxincome1 = 67900;
            double taxrate0 = (taxincome0 * 0.10);
            double taxrate1 = ((taxincome1 - taxincome0) * 0.15);       
            if (income <= taxincome0){
                tax = income * 0.10;
            }
            
            else if (income <= taxincome1){
                tax = taxrate0 + (income - 8350) *0.15;
}
}

为此:

double tax = 0;
    int taxincome0 = 0;
    int taxincome1 = 0;
    double taxrate0 = (taxincome0 * 0.10);
    double taxrate1 = ((taxincome1 - taxincome0) * 0.15);
    
    if (status ==0) {
        taxincome0 = 8350;
        taxincome1 = 33950;

        if (income <= taxincome0){
            tax = income * 0.10;
        }
                
        else if (income <= taxincome1){
            tax = taxrate0 + (income - 8350) *0.15;
        }
        }

    else if (status ==1) {
        
        taxincome0 = 16700;
        taxincome1 = 67900;
        
        if (income <= taxincome0){
            tax = income * 0.10;
        }
        
        else if (income <= taxincome1){
            tax = taxrate0 + (income - taxincome0) *0.15;
        }

使if-else语句直接捕获 double taxrate1 = ((taxincome1 - taxincome0) * 0.15);

*更新 我试过在进入if-else语句前先声明,会显示计算错误但不弹出错误

如归档Status:0 应纳税所得额:100000 正确的税收计算应该是 21720.0 但是我的代码显示 4970.000000000001

您可以将代码放在 if-else 语句之外。甚至你可以定义 if-else 之外的所有变量。你应该做的问题是,如果你需要变量存在于 if-else.

之外

如果你只使用if-else里面的变量,你应该在里面声明它以减少消耗内存。

double tax = 0;
double taxrate0 = (taxincome0 * 0.10);
double taxrate1 = ((taxincome1 - taxincome0) * 0.15);
/*
The integer and double shows 2009 U.S. Federal Personal Tax Rates
It might have change every year by different amount and different Tax Rates
Program
*/

int taxincome0 = 8350;
int taxincome1 = 33950;

if (status ==0) {
    
    if (income <= taxincome0){
        tax = income * 0.10;
    }
    
    else if (income <= taxincome1){
        tax = taxrate0 + (income - 8350) *0.15;
    }
}

*用户更新后

} else if (income <= taxincome1) {
    /* Value of income in this moment is 0. You are doing 0 - 8350 */
    tax = taxrate0 + (income - 8350) *0.15;
}
}

它应该工作:

double tax = 0;
int taxincome0 = 8350;
int taxincome1 = 33950;
double taxrate0 = (taxincome0 * 0.10);
double taxrate1 = ((taxincome1 - taxincome0) * 0.15);

if (status == 0) {
    if (income <= taxincome0) {
       tax = income * 0.10;
   } else if (income <= taxincome1) {
    tax = taxrate0 + (income - 8350) *0.15;
   }

}