首页 > 解决方案 > 关于我的 JavaScript 税务问题的问题

问题描述

我正在尝试就我正在创建的用于计算税收的 JavaScript 获得一些帮助。感觉我的数学有问题,但我认为代码按预期工作。当我输入这 2 个值时,得出的数学运算似乎并不准确。如果有人可以看看它,看看我的工作有什么问题,那将不胜感激。

package taxableIncome;
import java.util.Scanner;

public class taxableIncome {
    public static void main(String[] args) {
        System.out.println("Enter filing status: ");
        Scanner input = new Scanner(System.in);
        double x = input.nextDouble();
        if (x == 0) {
            x = 'S';
        } else if (x == 1) {
            x = 'M';
        } else if (x == 2) {
            x = 'J';
        } else if (x == 3) {
            x = 'H';
            }

        System.out.println("Enter taxable income: ");
        input = new Scanner(System.in);
        double y = input.nextInt();
        if (x == 'S') {
        if (y <= 8350) {
            System.out.println(y*.01);
        } else if (y >= 8351 && y<= 33950) {
            System.out.println(y*.15);
        } else if (y >= 33951 && y<= 82250) {
            System.out.println(y*.25);
        } else if (y >= 82251 && y<= 171550) {
            System.out.println(y*.28);
        } else if (y >= 171551 && y<= 372950) {
            System.out.println(y*.33);
        } else System.out.println(y*.35);
        }
        if (x == 'M') {
        if (y <= 8350) {
            System.out.println(y*.01);
        } else if (y >= 8351 && y<= 33950) {
            System.out.println(y*.15);
        } else if (y >= 33951 && y<= 68525) {
            System.out.println(y*.25);
        } else if (y >= 68526 && y<= 104425) {
            System.out.println(y*.28);
        } else if (y >= 104426 && y<= 186475) {
            System.out.println(y*.33);
        } else System.out.println(y*.35);
        }
        if (x == 'J') {
        if (y <= 16700) {
            System.out.println(y*.01);
        } else if (y >= 16701 && y<= 67900) {
            System.out.println(y*.15);
        } else if (y >= 67901 && y<= 137050) {
            System.out.println(y*.25);
        } else if (y >= 137051 && y<= 208850) {
            System.out.println(y*.28);
        } else if (y >= 208851 && y<= 372950) {
            System.out.println(y*.33);
        } else System.out.println(y*.35);
        }
        if (x == 'H') {
        if (y <= 11950) {
            System.out.println(y*.01);
        } else if (y >= 11951 && y<= 45500) {
            System.out.println(y*.15);
        } else if (y >= 45501 && y<= 117450) {
            System.out.println(y*.25);
        } else if (y >= 117451 && y<= 190200) {
            System.out.println(y*.28);
        } else if (y >= 190201 && y<= 372590) {
            System.out.println(y*.33);
        } else System.out.println(y*.35);
}

    }
}

标签: java

解决方案


  1. 我更改输出以向用户提示他可以输入的内容。
  2. 而不是读取双重更改它以存储第一个字符,因为那是您用来确定状态的内容。
  3. 重命名了您的 x 和 y,因为我不记得所有 x 和 y 的含义。
  4. 从用户输入中读取 double 而不是 int。

public class taxableIncome {
  public static void main(String[] args) {
    System.out.println("Enter filing status (S,M,J,H): ");
    Scanner input = new Scanner(System.in);
    char status = input.nextLine().toUpperCase().charAt(0);
    System.out.println("Enter taxable income: ");
    input = new Scanner(System.in);
    double taxableIncome = input.nextDouble();
    if (status == 'S') {
      if (taxableIncome <= 8350) {
        System.out.println(taxableIncome * .01);
      } else if (taxableIncome >= 8351 && taxableIncome <= 33950) {
        System.out.println(taxableIncome * .15);
      } else if (taxableIncome >= 33951 && taxableIncome <= 82250) {
        System.out.println(taxableIncome * .25);
      } else if (taxableIncome >= 82251 && taxableIncome <= 171550) {
        System.out.println(taxableIncome * .28);
      } else if (taxableIncome >= 171551 && taxableIncome <= 372950) {
        System.out.println(taxableIncome * .33);
      } else
        System.out.println(taxableIncome * .35);
    }
    if (status == 'M') {
      if (taxableIncome <= 8350) {
        System.out.println(taxableIncome * .01);
      } else if (taxableIncome >= 8351 && taxableIncome <= 33950) {
        System.out.println(taxableIncome * .15);
      } else if (taxableIncome >= 33951 && taxableIncome <= 68525) {
        System.out.println(taxableIncome * .25);
      } else if (taxableIncome >= 68526 && taxableIncome <= 104425) {
        System.out.println(taxableIncome * .28);
      } else if (taxableIncome >= 104426 && taxableIncome <= 186475) {
        System.out.println(taxableIncome * .33);
      } else
        System.out.println(taxableIncome * .35);
    }
    if (status == 'J') {
      if (taxableIncome <= 16700) {
        System.out.println(taxableIncome * .01);
      } else if (taxableIncome >= 16701 && taxableIncome <= 67900) {
        System.out.println(taxableIncome * .15);
      } else if (taxableIncome >= 67901 && taxableIncome <= 137050) {
        System.out.println(taxableIncome * .25);
      } else if (taxableIncome >= 137051 && taxableIncome <= 208850) {
        System.out.println(taxableIncome * .28);
      } else if (taxableIncome >= 208851 && taxableIncome <= 372950) {
        System.out.println(taxableIncome * .33);
      } else
        System.out.println(taxableIncome * .35);
    }
    if (status == 'H') {
      if (taxableIncome <= 11950) {
        System.out.println(taxableIncome * .01);
      } else if (taxableIncome >= 11951 && taxableIncome <= 45500) {
        System.out.println(taxableIncome * .15);
      } else if (taxableIncome >= 45501 && taxableIncome <= 117450) {
        System.out.println(taxableIncome * .25);
      } else if (taxableIncome >= 117451 && taxableIncome <= 190200) {
        System.out.println(taxableIncome * .28);
      } else if (taxableIncome >= 190201 && taxableIncome <= 372590) {
        System.out.println(taxableIncome * .33);
      } else
        System.out.println(taxableIncome * .35);
    }
  }
}

推荐阅读