我正在为班级制作税收计算器,但我不太清楚如何在个别情况下分离我的代码
println("Please enter your filing status: ");
println("Enter 0 for single filers,");
println(" 1 for married filing jointly,");
println(" 2 for married filing separately, or");
println(" 3 for head of household");
double tax = 0;
DecimalFormat df = new DecimalFormat("#.00");
int filingStatus = readInt("Status: ");
switch (filingStatus) {
case 0: double taxableIncomes = readDouble("Please enter your taxable income: ");
if (taxableIncomes <= 8925)
{
tax = (taxableIncomes * TEN);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 8925 && taxableIncomes <= 36250)
{
tax = 892.5 + FIFTEEN * (taxableIncomes - 8925);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 36250 && taxableIncomes <= 87850)
{
tax = 892.5 + 4098.75 + TWENTYFIVE * (taxableIncomes - 36250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 87850 && taxableIncomes <= 183250)
{
tax = 892.5 + 4098.75 + 12900 + TWENTYEIGHT * (taxableIncomes - 87850);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 183250 && taxableIncomes <= 398350)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + THIRTYTHREE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 398350 && taxableIncomes <= 400000)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + 70983 + THIRTYFIVE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 400000)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + 70983 + 577.5 + THIRTYNINE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
break;
进入单独的方法,然后调用 case:0 下的方法。对于我的计算器,我应该使用四种不同的方法,因为我有四种不同的情况,所以我认为要做的事情是轻松地将每种情况的代码分类为不同的方法,然后为我拥有的每种情况调用这些方法。如果我尝试使用
private double tax()
并复制并粘贴 case 0 下的所有代码:它告诉我预期的类、接口或枚举。