Java税务计算器[已关闭]

9rygscc1  于 2023-03-06  发布在  Java
关注(0)|答案(2)|浏览(116)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我带回家的挑战是写一个计算所得税的程序。作业必须分步骤完成,每完成一步我就会得到积分。

第2步(3分)

根据状态标识扣减额:(1)单身= $12,000或(2)已婚= $24,000。如果未输入为1或2,则将状态设置为1。计算应纳税收入(AGI -扣减)。如果为负数,则将应纳税收入设置为零。输出扣减和应纳税收入。提交以进行评级,以确认通过五项测试。

import java.util.Scanner;

public class LabProgram {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      int wages = scnr.nextInt();
      int interest = scnr.nextInt();
      int unemployment = scnr.nextInt();
      int status = scnr.nextInt();
      int withheld = scnr.nextInt();

      int AGI = wages + interest + unemployment;
      int singleDeduct = 12000;
      int marriedDeduct = 24000;
      int taxableIncome = 0;

      if (AGI > 120000)
      {
         System.out.printf("AGI: $%,2d\n", AGI);
         System.out.println("Error: Income too high to use this form");
      }
      else if (status == 1)
      {
         taxableIncome = AGI - singleDeduct;
         System.out.printf("AGI: $%,2d\n", AGI);
         System.out.printf("Deduction: $%,2d\n", singleDeduct);
         System.out.printf("Taxable income: $%,2d", taxableIncome);
      }
      else if (status == 2)
      {
         taxableIncome = 0;
         System.out.printf("Deduction: $%,2d\n", marriedDeduct);
         System.out.printf("Taxable income: $%,d", taxableIncome);
      }
   }
}

我理解这个问题的方式是,我已经为单身扣除额12000和已婚扣除额24000创建了变量。
我感到困惑的是设置“单身”或“已婚”的状态
我的逻辑是:

If single is equal to one:
deductions = 12000;
taxableIncome = AGI - deductions;

Else If married is equal to two:
deductions = 24000;
taxableIncome = 0; "since it is negative when I input the numbers to my calculator"

完成这一步后,我应该有5分,但目前有4分,这意味着我还没有完全满足步骤2。

vqlkdk9b

vqlkdk9b1#

几点建议:
1.你只有在单身时才能扣除扣除。
1.就像肖恩在他的回答中说的,如果不等于1或2,你就不会认为自己是单身。
下面是一种同时实现这两种功能的方法

// set deduction to 24000 for married
// otherwise 12000 (assume single)
int deduction = status == 2 ? 24000 : 12000;
// if deduction is higher than AGI
// set taxable income to 0
taxableIncome = deduction > AGI ? 0 : AGI - deduction;

这使用了三元运算符,本质上相当于说

int deduction;
if (status == 2)
    deduction = 24000;
else
    deduction = 12000;

if (deduction > AGI)
    taxableIncome = 0;
else
    taxableIncome = AGI - deduction;

除了当AGI〉120000时的错误消息之外,我会等到计算完成后再打印任何内容,这将有助于减少打印语句的数量,当AGI〉120000时(在错误消息之后),您也可以调用System.exit(0);来立即停止程序。

vdzxcuhz

vdzxcuhz2#

看起来你错过了这一步?
如果未输入1或2,则将状态设置为1。
试试这个?

if (AGI > 120000) {
    System.out.printf("AGI: $%,2d\n", AGI);
    System.out.println("Error: Income too high to use this form");
}
else if (status == 2) {
    taxableIncome = 0;
    System.out.printf("Deduction: $%,2d\n", marriedDeduct);
    System.out.printf("Taxable income: $%,d", taxableIncome);
}
// will default to status 1
else {
    taxableIncome = AGI - singleDeduct;
    System.out.printf("AGI: $%,2d\n", AGI);
    System.out.printf("Deduction: $%,2d\n", singleDeduct);
    System.out.printf("Taxable income: $%,2d", taxableIncome);
}

相关问题