的更新問題
對於流量的目的,應先查詢更新回答OU繞開循環,然後在循環結束時再次查詢以準備下一次迭代。你的訂單略有偏差,所以會發生奇怪的事情,但大部分代碼是正確的。
// Get the first Customer ID
System.out.println("Enter Customer ID: ");
customerID = input.nextInt();
while(customerID != -1)
{
// Get income and withholding information
System.out.print("Enter Income: ");
income = input.nextDouble();
System.out.print("Enter Federal Taxes Withheld: ");
federalwh = input.nextDouble();
System.out.print("Enter State Taxes Withheld: ");
statewh = input.nextDouble();
System.out.print("Enter Deductions: ");
deduction = input.nextDouble();
//!IMPORTANT SET all values for bracket to zero here!
bracket10to20 = 0.0;
//you can fill in all the rest
//put all the calculations here from original answer
//Get next customer id
System.out.print("Enter Customer ID: ");
customerID = input.nextInt();
// if it's -1, we won't go through while again
}
爲原始的問題
原來的答覆這裏的問題行:
if(taxableIncome > 20000 && taxableIncome <= 40000);
分號打破它。刪除。但是,我建議你將你的方法改爲其他ifs。你嵌套很多,如果/別人的一起,當你實際上可以把它們連這樣的:
taxableIncome = income - deduction;
if (taxableIncome <= 10000) {
federalTax = 0.0;
} else if (taxableIncome > 10000 && taxableIncome <= 20000) {
bracket10to20 = (taxableIncome - 10000);
} else if (taxableIncome > 20000 && taxableIncome <= 40000) {
bracket20to40 = taxableIncome - 20000;
bracket10to20 = 10000;
} else if (taxableIncome > 40000) {
bracket40plus = taxableIncome - 30000;
bracket10to20 = 10000;
bracket20to40 = 20000;
}
federalTax = (bracket10to20 * 0.15) + (bracket20to40 * 0.2)
+ (bracket40plus * 0.3);
這是顯著更容易閱讀和你沒有跟蹤所有的嵌套。與往常一樣,格式化可以減少代碼中出現錯誤的機會,並使其他人更容易提供幫助。
修復格式,你可能會發現什麼是錯的。 – clcto 2014-09-24 16:39:09
只是一個提示(與問題無關):不要使用浮點變量(即雙精度)進行貨幣計算。請參閱http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – 2014-09-24 16:43:30
謝謝,我知道我的格式化已關閉,但我是新手,正在嘗試。我的教授希望我們用雙。但是,除了你的所有建議,我很欣賞給出的任何反饋。 – guinea2 2014-09-24 17:27:44