我不確定如何讓我的代碼正確輸出正確的輸出。以下是我迄今爲止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
問題是什麼?你能舉一個當前輸出的例子嗎? –
我現在有一個錯誤與我的「如果聲明說變量狀態可能尚未初始化 – Tanner10
@ Tanner10首先在谷歌鏈接」變量未初始化的Java「http://stackoverflow.com/questions/2448843/variable-可能未被初始化錯誤 – sashkello