我剛剛在大學完成了一項Java測試,我知道我已經回答了一個錯誤的特定問題,並且希望得到一些幫助/澄清?計算應納稅所得額Java
的問題如下:
實現一個需要某人的收入和工作出稅的方法。 如果這個人的收入低於7500,那麼稅收= 0. 如果這個人的收入在7501和45000之間,那麼稅額= 20%,減去免稅的7500。 最後,如果這個人的收入高於45001,那麼稅收= 40%,減去20%支出的收入,然後減去免稅的7500。
由於時間不長,我只是做了一個基本的if else語句,顯示了收入和稅收括號,下面的例子。
public static double incomeTax(double income){
if(income <= 7500){
income = income * 0;
}
else if(income >= 7501 && income <= 45000){
income = income * 0.8;
}
else(income >= 45001){
income = income * 0.6;
}
return income;
}
我知道這些代碼是不正確的,沒有在附近,但因爲它是來測試結束時,我的希望給了它一個去只是爲了獲得針對如果else語句的標誌。
我真的很感謝這裏的幫助。
謝謝。
後很好的意見,這是我回來了(與幫助!頗多:])...
import java.util.Scanner;
public class TaxableIncome
{
public static void main(String[] args){
netIncome();
}
public static double netIncome() {
double income = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter income: ");
income = in.nextDouble();
System.out.println();
double tax1 = 0;
double tax2 = 0;
double totalTax = tax1 + tax2;
// high income bracket
if (income > 45000) {
double part1 = income - 45000; // part = income - 45000
tax1 += part1 * 0.4; // tax = tax + part * 0.4
System.out.println("High Tax Band - Greater than 45000: " + tax1);
}
// medium income bracket
if (income > 7500) {
double part2 = income - 7500;
tax2 += part2 * 0.2;
System.out.println("Medium Tax Band - Greater than 7500: " + tax2);
}
System.out.println("Total Tax = " + (tax1 + tax2));
// tax for low income is zero, we don't need to compute anything.
return totalTax;
}
}
它說:「如果那個人在7501和45000之間賺錢,那麼稅收= 20%',但是你把'收入'乘以8,爲什麼?我認爲這比數據編程更適合你的情況。 –
是的,我很受打擊... 雖然感謝。其實,是的,總是搞砸了。 – PrimalScientist
請勿使用'double'或'float'作爲貨幣價值。錢是固定的十進制數,不是浮點數。而是用最小的面額來表示你的貨幣價值(美國的前幾分錢)。 – DwB