**8.12(金融应用:计算税款)使用数组重写程序清单3-5
题目
题目描述
**8.12(金融应用:计算税款)使用数组重写程序清单3-5。
每个纳税人身份都有6种税率,每种税率都应用在某个特定范围内的可征税收入。
比如,对于一个单身纳税人,六种税率可以用下面的数组表示:
double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
所有纳税人身份针对各个税率的纳税等级可以用一个二维数组表示,如下所示:
int[][] brackets = {
{8350, 39500, 82250, 171550, 372950}, // Single filer(0)
{16700, 69000, 137050, 20885, 372950}, // Married jointly qualifying widow(er)(1)
{8350, 33950, 68525, 104425, 186475}, // Married separately(2)
{11950, 45500, 117450, 190200, 372950}, // Head of household(3)
};
假设单身身份的纳税人的可征税收入是400 000美元,则税收可以如下计算:
tax = brackets[0][0] * rates[0] +
(brackets[0][1] - brackets[0][0]) * rates[1] +
(brackets[0][2] - brackets[0][1]) * rates[2] +
(brackets[0][3] - brackets[0][2]) * rates[3] +
(brackets[0][4] - brackets[0][3]) * rates[4] +
(400000 - brackets[0][4]) * rates[5];
程序清单3-5:补充完整版
import java.util.Scanner;
public class QingDan {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter filing status
System.out.println("(0-single filer, 1-married jointly or " +
"qualifying widow(er), 2-married separately, 3-head of " +
"household) Enter the filing status:");
int status = input.nextInt();
// Prompt the user to enter taxable income
System.out.println("Enter the taxable income:");
double income = input.nextDouble();
// compute tax
double tax = 0;
if (status == 0) {
// Compute tax for single filers
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 + (income - 8350) * 0.15 +
(income - 33950) * 0.25;
else if(income <= 171550)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15