2014-02-11 169 views
-4

我不確定如何讓我的代碼正確輸出正確的輸出。以下是我迄今爲止Java變量可能未被初始化

import java.util.Scanner; 
public class ComputeTax { 
    public static void main(String[] args) { 
     System.out.print("Taxable Income Single  Married Jointly  Married Seperate  Head of House"); 
     System.out.print("\n\n-----------------------------------------------------------------------------------"); 
     int status; 
     double income; 
     double tax = 0; 
     for(income = 50000; income <= 60000; income+=50) 
     { 
      if (status == 50000) 
      { 
       if (income <= 8350) 
        tax = income * 0.10; 
       else if (income <= 33950) 
        tax = 8350 * 0.10 + (income - 8350) * 0.15; 
       else if (income <= 82250) 
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 
        (income - 33950) * 0.25; 
       else if (income <= 171550) 
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 
        (82250 - 33950) * 0.25 + (income - 82250) * 0.28; 
       else if (income <= 372950) 
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 
        (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + 
        (income - 171550) * 0.33; 
       else 
        tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + 
        (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + 
        (372950 - 171550) * 0.33 + (income - 372950) * 0.35; 
      } 
      else if (status == 1) { // Compute tax for married file jointly 
       // Left as exercise 
      } 
      else if (status == 2) { // Compute tax for married separately 
       // Left as exercise 
      } 
      else if (status == 3) { // Compute tax for head of household 
       // Left as exercise 
      } 


      // Display the result 
      System.out.println("Tax is " + (int)(tax * 100)/100.0); 
     } 
    } 
} 

樣本輸出是:

Taxable Income  Single  Married  Married Seperate   Head of House 
50000     8688  6665  8688      7352 
50050     8700  6673  8700      7365 
... 
59950     11175  8158  11175     9840 
60000     11188  8165  11188     9852 

我相信我的「for循環」是正確的,我只是不知道我的計算,以及如何將它們全部打印出來。目前,我收到錯誤:

variable status might not have been initialized

+1

問題是什麼?你能舉一個當前輸出的例子嗎? –

+0

我現在有一個錯誤與我的「如果聲明說變量狀態可能尚未初始化 – Tanner10

+4

@ Tanner10首先在谷歌鏈接」變量未初始化的Java「http://stackoverflow.com/questions/2448843/variable-可能未被初始化錯誤 – sashkello

回答

0

在'for'循環中,您有一個'if'語句使用status整型變量。您正在計算數字值的狀態,如狀態== 50000,但您從未真正將狀態值置於狀態變量。你沒有初始化類型,但沒有值。嘗試像

status = 18000; 

或之前的for循環。

希望我幫助, 賈斯汀

+0

感謝賈斯汀我把我的int狀態= 18000和我的if(狀態== 50000)它編譯,但我似乎無法得到它打印出我想要它看起來像我的示例輸出。 – Tanner10

+0

@ Tanner10:你可以添加喲你的代碼和當前輸出到你的文章(ideone.com)?此外,18000是一個完全任意的數字。添加一些可以用if語句進行實際編譯的東西,或者添加一些其他的if語句,因爲它具有更大的範圍。 –

+0

當然讓我做一個帳戶 – Tanner10