我刚刚在大学完成了 Java 测试,我知道我回答了一个特定的问题,需要一些帮助/澄清吗?
问题如下:
实现一种方法,根据某人的收入计算出税收。 如果那个人的收入低于 7500,则税收 = 0。 如果那个人的收入在 7501 到 45000 之间,那么税 = 20%,减去 7500,这是免税的。 最后,如果该人的收入超过 45001,那么税收 = 40%,减去 20% 范围内的收入,然后减去免税的 7500。
由于时间紧迫,我只是做了一个基本的 if else 语句来显示收入和税级,示例如下。
public static double incomeTax(double income){
if(income <= 7500){
income = income * 0;
}
else if(income >= 7501 && income <= 45000){
income = income * 0.8;
}
else(income >= 45001){
income = income * 0.6;
}
return income;
}
我知道代码不正确,一点都不正确,但是在测试快结束时我试了一下,希望能为 if else 语句打分。
我真的很感激这里的任何帮助。
谢谢。
在得到很好的反馈后,这就是我回来的结果(有很多帮助!!:])...
import java.util.Scanner;
public class TaxableIncome
{
public static void main(String[] args){
netIncome();
}
public static double netIncome() {
double income = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter income: ");
income = in.nextDouble();
System.out.println();
double tax1 = 0;
double tax2 = 0;
double totalTax = tax1 + tax2;
// high income bracket
if (income > 45000) {
double part1 = income - 45000; // part = income - 45000
tax1 += part1 * 0.4; // tax = tax + part * 0.4
System.out.println("High Tax Band - Greater than 45000: " + tax1);
}
// medium income bracket
if (income > 7500) {
double part2 = income - 7500;
tax2 += part2 * 0.2;
System.out.println("Medium Tax Band - Greater than 7500: " + tax2);
}
System.out.println("Total Tax = " + (tax1 + tax2));
// tax for low income is zero, we don't need to compute anything.
return totalTax;
}
}
最佳答案
一个简单的答案是这样的:
public static double netIncome(double income) {
double tax = 0;
// high income bracket
if (income > 45000) {
double part = income - 45000;
tax += part * 0.4;
income = 45000;
}
// medium income bracket
if (income > 7500) {
double part = income - 7500;
tax += part * 0.2;
}
// tax for low income is zero, we don't need to compute anything.
return tax;
}
通过这种方式,您可以计算每个税级的税额并将它们相加。
关于java - 计算应纳税所得额 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14739094/