2014-09-24 45 views
-2

我正在寫一個程序,我需要輸入以下內容:客戶ID,他們的收入,聯邦扣繳,州扣稅和扣除。但是,當我運行我的程序時,它會詢問我的客戶ID,那就是......我不確定這裏有什麼不正確。Java稅務程序幫助讚賞 - 哨兵while循環

// Get first Customer ID 
{ while(customerID != -1) 
{ 
    System.out.print("Enter Customer ID: "); 
    customerID = input.nextInt(); 
    // 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(); 
} 

    // Get next Customer ID 
System.out.println("Enter Customer ID: "); 
    customerID = input.nextInt(); 
+0

修復格式,你可能會發現什麼是錯的。 – clcto 2014-09-24 16:39:09

+0

只是一個提示(與問題無關):不要使用浮點變量(即雙精度)進行貨幣計算。請參閱http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – 2014-09-24 16:43:30

+0

謝謝,我知道我的格式化已關閉,但我是新手,正在嘗試。我的教授希望我們用雙。但是,除了你的所有建議,我很欣賞給出的任何反饋。 – guinea2 2014-09-24 17:27:44

回答

1

嘗試刪除分號這裏

if(taxableIncome > 20000 && taxableIncome <= 40000); 

應該

if(taxableIncome > 20000 && taxableIncome <= 40000) 
+0

非常感謝您提供完成我的代碼的其他提示嗎?我是新來的,我卡住我知道我需要一個循環,但不知道在哪裏,以及程序的其餘部分有沒有指針? – guinea2 2014-09-24 16:50:46

0

的更新問題

對於流量的目的,應先查詢更新回答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); 

這是顯著更容易閱讀和你沒有跟蹤所有的嵌套。與往常一樣,格式化可以減少代碼中出現錯誤的機會,並使其他人更容易提供幫助。

+0

謝謝我知道我的格式是一團糟,我很努力但很新。我的教授認爲他很有幫助,但他讓它更加混亂。對項目的其他部分有什麼幫助?我不是在尋找答案只是指導。 – guinea2 2014-09-24 16:51:48

+0

@ guinea2 Java IDE中應該有一個選項來格式化或自動格式化。使用它可以更容易地遵循編碼路徑。有關重複,請參見[this](http://stackoverflow.com/questions/12999899/getting-user-input-with-scanner)。如果你將大部分代碼打包到while循環中,它應該可以幫助你朝着正確的方向發展。 – Compass 2014-09-24 16:55:31

+0

我發現格式按鈕,這是非常有幫助的。我查看了這個鏈接,但是我開始時(customerId!= -1)卻讓人感到困惑,但它仍然只是要求我輸入客戶ID。我很感激你幫助我。我正試圖將其分解成單獨的部分。 – guinea2 2014-09-24 17:44:09