1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35}; 8 int[][] brackets = 9 { 10 {8350, 33950, 82250, 171550, 372950}, 11 {16700, 67900, 137050, 208850, 372950}, 12 {8350, 33950, 68525, 104425, 186475}, 13 {11950, 45500, 117450, 190200, 372950} 14 }; 15 16 Scanner input = new Scanner(System.in); 17 System.out.print("Input the number of the people(0 to 3): "); 18 int person = input.nextInt(); 19 System.out.print("Input the income: "); 20 int income = input.nextInt(); 21 22 input.close(); 23 24 double tax = computeTax(brackets, rates, person, income); 25 System.out.println("The tax is " + tax); 26 } 27 28 public static double computeTax(int[][] brackets, double[] rates, int person, int income) 29 { 30 int largerCount = 0; 31 for(int i = 0; i < brackets[person].length; i++) 32 { 33 if(brackets[person][i] < income) 34 largerCount++; 35 else 36 break; 37 } 38 39 if(largerCount == 0) 40 return 0; 41 else if(largerCount == 1) 42 return (income - brackets[person][0]) * rates[0]; 43 else 44 { 45 int tax = 0; 46 tax += (income - brackets[person][largerCount - 1]) * rates[largerCount]; 47 largerCount--; 48 while(largerCount > 0) 49 { 50 tax += (brackets[person][largerCount] - brackets[person][largerCount - 1]) * rates[largerCount]; 51 largerCount--; 52 } 53 tax += brackets[person][0] * rates[0]; 54 return tax; 55 } 56 } 57 }
时间: 2024-10-26 03:20:16