英文:
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 <= 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;
}
}
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 <= 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;
}
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 <= taxincome0){
tax = income * 0.10;
}
else if (income <= taxincome1){
tax = taxrate0 + (income - 8350) *0.15;
}
}
*after updated by user
} else if (income <= 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 <= taxincome0) {
tax = income * 0.10;
} else if (income <= taxincome1) {
tax = taxrate0 + (income - 8350) *0.15;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论