Java黑皮书课后题第8章:**8.12(金融应用:计算税款)使用数组重写程序清单3-5,每个纳税人身份都有6种税率。每种税率都应用在某个特定范围内的可征税收入
**8.12(金融應(yīng)用:計算稅款)使用數(shù)組重寫程序清單3-5
- 題目
- 題目描述
- 程序清單3-5:補充完整版
- 代碼
題目
題目描述
**8.12(金融應(yīng)用:計算稅款)使用數(shù)組重寫程序清單3-5。
每個納稅人身份都有6種稅率,每種稅率都應(yīng)用在某個特定范圍內(nèi)的可征稅收入。
比如,對于一個單身納稅人,六種稅率可以用下面的數(shù)組表示:
double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
所有納稅人身份針對各個稅率的納稅等級可以用一個二維數(shù)組表示,如下所示:
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)
};
假設(shè)單身身份的納稅人的可征稅收入是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 ScannerScanner input = new Scanner(System.in);// Prompt the user to enter filing statusSystem.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 incomeSystem.out.println("Enter the taxable income:");double income = input.nextDouble();// compute taxdouble tax = 0;if (status == 0) { // Compute tax for single filersif (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 +(82250 - 33950) * 0.25 + (income - 82250) * 0.28;else if(income <= 372950)tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 +(171550 - 82250) * 0.28 +(income - 171550) * 0.33;elsetax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 +(171550 - 82250) * 0.28 +(372950 - 171550) * 0.33 + (income - 372950) * 0.35;}else if(status == 1){ // Left as an exercise// Compute tax for married file jointly or qualifying widow(er)}else if(status == 2){ // Left as an exercise}else if(status == 3){ // Left as an exercise}else{System.out.println("Error: invalid status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int)(tax * 100) / 100.0);} }代碼
import java.util.Scanner;public class Test8_12 {public static void main(String[] args) {// Create a ScannerScanner input = new Scanner(System.in);// Prompt the user to enter filing statusSystem.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 incomeSystem.out.println("Enter the taxable income:");double income = input.nextDouble();// compute taxdouble tax = 0;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)};double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};// Compute tax for single filersif (status >= 0 && status <= 3) {if (income <= brackets[status][0])tax = income * rates[0];else if (income <= brackets[status][1])tax = brackets[status][0] * rates[0] + (income - brackets[status][0]) * rates[1];else if (income <= brackets[status][2])tax = brackets[status][0] * rates[0] + (brackets[status][1] - brackets[status][0]) * rates[1] +(income - brackets[status][1]) * rates[2];else if (income <= brackets[status][3])tax = brackets[status][0] * rates[0] + (brackets[status][1] - brackets[status][0]) * rates[1] +(brackets[status][2] - brackets[status][1]) * rates[2] + (income - brackets[status][2]) * rates[3];else if (income <= brackets[status][4])tax = brackets[status][0] * rates[0] + (brackets[status][1] - brackets[status][0]) * rates[1] +(brackets[status][2] - brackets[status][1]) * rates[2] + (brackets[status][3] - brackets[status][2]) * rates[3] +(income - brackets[status][3]) * rates[4];elsetax = brackets[status][0] * rates[0] + (brackets[status][1] - brackets[status][0]) * rates[1] +(brackets[status][2] - brackets[status][1]) * rates[2] + (brackets[status][3] - brackets[status][2]) * rates[3] +(brackets[status][4] - brackets[status][3]) * rates[4] + (income - brackets[status][4]) * rates[5];} else {System.out.println("Error: invalid status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int)(tax * 100) / 100.0);} }總結(jié)
以上是生活随笔為你收集整理的Java黑皮书课后题第8章:**8.12(金融应用:计算税款)使用数组重写程序清单3-5,每个纳税人身份都有6种税率。每种税率都应用在某个特定范围内的可征税收入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第8章:**8.11
- 下一篇: Java黑皮书课后题第8章:*8.13(