2015-09-09 3 views
1

Я делаю таблицу налогов, и все работает правильно. Однако у меня возникают проблемы с расстоянием в теле стола. Интервал под облагаемым налогом доходом, одиночная и женатая совместная или квалифицирующая вдова (э-э) прекрасна, но когда я добираюсь до раздельного брака и главы семьи, промежуток времени отключается. Любая идея, что я могу сделать, чтобы исправить это? Это то, что я до сих пор:Интервал на таблице налогов Java

//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; 
     } 
} 

ответ

1

Вы можете добавить отсчет статус на ваш интервал так:

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 ... 

Я надеюсь, что это помогает с заданием расстояния.

+0

спасибо, что исправили проблему отступы у меня был. – sarahm

1

Вам необходимо некоторое дополнительное пустое пространство после «% 12.0f» на линии 23:

import java.util.Scanner; 

//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; 
     } 
} 
+0

Спасибо. Я пытался сделать что-то подобное раньше, но это не сработало бы для меня. Я, должно быть, пропустил двойную цитату или что-то в этом роде, потому что, когда я это делал, делал именно то, что хотел. – sarahm

Смежные вопросы