gpt4 book ai didi

java - Java 税表上的间距

转载 作者:行者123 更新时间:2023-11-30 03:16:23 24 4
gpt4 key购买 nike

我正在制作一个税表,一切都运行正常。但是,我在表格主体的间距方面遇到了麻烦。应税收入、单例、已婚联合或符合资格的寡妇(鳏夫)下的间距很好,但当我谈到已婚分居和户主时,间距就大了。知道我可以做什么来尝试纠正这个问题吗?这是我到目前为止所拥有的:

//this program prints a tax table
public class PrintTaxTable {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

//create table header
System.out.printf("%5s%12s%20s%12s%12s\n", "Taxable", "Single",
"Married Joint" , "Married", "Head of");
System.out.printf("%5s%20s%12s%12s%12s\n", "Income", "",
"or Qualifying", "Separate", "a house");
System.out.printf("%20s%15s%12s%20s%20s\n", "", "Widow(er)","", "","");
System.out.println("------------------------------------------------" +
"-----------------");

// Display table body
double taxableIncome = 50000;
while (taxableIncome <= 60000){
System.out.printf("%5.0f", taxableIncome);
for (int status = 0; status < 4; status++) {
// Display the product and align properly
System.out.printf("%12.0f", computetax(status, taxableIncome));
}
System.out.println("");
taxableIncome = taxableIncome + 50;
}
}
//compute all of the tax rates
public static double computetax(int status, double taxableIncome) {
double tax = 0;
if (status == 0) {
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(taxableIncome - 33950) * 0.25;
}
else if (status == 1) {
if (taxableIncome <= 16700)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 67900)
tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15;
}
else if (status == 2) {
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 68525)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(taxableIncome - 33950) * 0.25;
}
else if (status == 3) {
if (taxableIncome <= 11950)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 45500)
tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15;
else if (taxableIncome <= 117450)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(taxableIncome - 45500) * 0.25;
}
return tax;
}
}

最佳答案

您可以将状态计数添加到您的间距中,如下所示:

import java.util.Scanner;

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

//create table header
System.out.printf("%5s%12s%20s%12s%12s\n", "Taxable", "Single",
"Married Joint" , "Married", "Head of");
System.out.printf("%5s%20s%12s%12s%12s\n", "Income", "",
"or Qualifying", "Separate", "a house");
System.out.printf("%20s%15s%12s%20s%20s\n", "", "Widow(er)","", "","");
System.out.printf("------------------------------------------------" +
"-----------------\n");

// Display table body
double taxableIncome = 50000;
while (taxableIncome <= 60000){
System.out.printf("%5.0f", taxableIncome);
for (int status = 0; status < 4; status++) {
// Display the product and align properly
System.out.printf("%"+(12.0+(status+1))+"f", computetax(status, taxableIncome));
}
System.out.print("\n");
taxableIncome = taxableIncome + 50;
}
}
//compute all of the tax rates
public static double computetax(int status, double taxableIncome) {
double tax = 0;
if (status == 0) {
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(taxableIncome - 33950) * 0.25;
}
else if (status == 1) {
if (taxableIncome <= 16700)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 67900)
tax = 16700 * 0.10 + (taxableIncome - 16700) * 0.15;
}
else if (status == 2) {
if (taxableIncome <= 8350)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 33950)
tax = 8350 * 0.10 + (taxableIncome - 8350) * 0.15;
else if (taxableIncome <= 68525)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(taxableIncome - 33950) * 0.25;
}
else if (status == 3) {
if (taxableIncome <= 11950)
tax = taxableIncome * 0.10;
else if (taxableIncome <= 45500)
tax = 11950 * 0.10 + (taxableIncome - 11950) * 0.15;
else if (taxableIncome <= 117450)
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 +
(taxableIncome - 45500) * 0.25;
}
return tax;
}
}

输出如下:

Taxable      Single       Married Joint     Married     Head of
Income or Qualifying Separate a house
Widow(er)
-----------------------------------------------------------------
50000 8688 6665 8688 7353
50050 8700 6673 8700 7365 ...

我希望它有助于解决间距问题。

关于java - Java 税表上的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32482440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com