有没有任何方法可以在Java中if-else语句之外执行代码?

huangapple go评论69阅读模式
英文:

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

问题

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;
    }
}

taxrate1 = ((taxincome1 - taxincome0) * 0.15);

// update
// I have tried to declare it before entering the if-else statement,
// and it will display the wrong calculation but no error popping out
//
// As in Filing Status:0
// Taxable income: 100000
// The correct tax calculation should be 21720.0
// But my code shows 4970.000000000001
英文:

*edited
Is there any way to execute codes outside the if-else statement in java?
Make the if-else statement will take the code and amount before the if-statement, and execute it.
Like from this:

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 &lt;= taxincome0){
	        tax = income * 0.10;
	    }
	    
	    else if (income &lt;= 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 &lt;= taxincome0){
		        tax = income * 0.10;
		    }
		    
		    else if (income &lt;= taxincome1){
		        tax = taxrate0 + (income - 8350) *0.15;
}
}

To this:

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 &lt;= taxincome0){
            tax = income * 0.10;
        }
    		    
        else if (income &lt;= taxincome1){
            tax = taxrate0 + (income - 8350) *0.15;
        }
        }

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

Make the if-else statement will direct capture the
double taxrate1 = ((taxincome1 - taxincome0) * 0.15);

*update
I have tried to declare it before entering the if-else statement, and it will display the wrong calculation but no error popping out

As in Filing Status:0
Taxable income: 100000
The correct tax calculation should be 21720.0
But my code shows 4970.000000000001

答案1

得分: 0

以下是翻译好的内容:

你可以将代码放在if-else语句之外。甚至可以在if-else语句之外定义所有变量。你需要考虑的问题是是否需要让变量存在于if-else语句之外。

如果你只会在if-else语句内部使用这些变量,为了减少内存消耗,你应该在内部声明它。

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;
    }
}
英文:

You can put the code outside of if-else statement. Even you can define all variables outside of if-else. The question you should do is if you need that the variables exists outside of if-else.

If you only will use the variables inside of if-else you should declare it inside for reduce the consum memory.

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 &lt;= taxincome0){
        tax = income * 0.10;
    }
    
    else if (income &lt;= taxincome1){
        tax = taxrate0 + (income - 8350) *0.15;
    }
}

*after updated by user

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

IT SHOULD WORK:

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 &lt;= taxincome0) {
       tax = income * 0.10;
   } else if (income &lt;= taxincome1) {
    tax = taxrate0 + (income - 8350) *0.15;
   }

}

huangapple
  • 本文由 发表于 2020年10月23日 17:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定