2014-02-11 73 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