簡體   English   中英

無論如何要在java中的if-else語句之外執行代碼?

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

*edited 有沒有辦法在java中的if-else語句之外執行代碼? make 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);

*update 我嘗試在進入 if-else 語句之前聲明它,它會顯示錯誤的計算但沒有彈出錯誤

如在申報狀態: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;
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM