我只会用最原始的思路写,抛砖引玉吧
class Tax
{
public static void main(String[] args)
{
System.out.println(getTax(5800));
}
public static double getTax(double sal)
{
double t=0;
double b =sal-800;
if(b<=500)//25
{
t=b*0.05;
return t;
}
else if(b<=2000)//175
{
t=25+(b-500)*0.1;
return t;
}
else if(b<=5000)//625
{
t=175+(b-2000)*0.15;
return t;
}
else if(b<=20000)//3625
{
t=625+(b-5000)*0.2;
return t;
}
else if(b<=40000)//8625
{
t=3625+(b-20000)*0.25;
return t;
}
else if(b<=60000)//14625
{
t=8625+(b-40000)*0.3;
return t;
}
else if(b<=80000)//21625
{
t=14625+(b-60000)*0.35;
return t;
}
else if(b<=100000)//29625
{
t=21625+(b-80000)*0.4;
return t;
}
else
{
t=29625+(b-100000)*0.45;
return t;
}
}
}
class Tax
{
public static void main(String[] args)
{
System.out.println(getTax(5800));
}
public static double getTax(double sal)
{
double t=0;
double b =sal-800;
if(b<=500)//25
{
t=b*0.05;
return t;
}
else if(b<=2000)//175
{
t=25+(b-500)*0.1;
return t;
}
else if(b<=5000)//625
{
t=175+(b-2000)*0.15;
return t;
}
else if(b<=20000)//3625
{
t=625+(b-5000)*0.2;
return t;
}
else if(b<=40000)//8625
{
t=3625+(b-20000)*0.25;
return t;
}
else if(b<=60000)//14625
{
t=8625+(b-40000)*0.3;
return t;
}
else if(b<=80000)//21625
{
t=14625+(b-60000)*0.35;
return t;
}
else if(b<=100000)//29625
{
t=21625+(b-80000)*0.4;
return t;
}
else
{
t=29625+(b-100000)*0.45;
return t;
}
}
}
{
public static final double LOWSALARY=800.0;
public static final double[][] TAX={
{0.0,500.0,0.05},
{500.0,2000.0,0.10},
{2000.0,5000.0,0.15},
{5000.0,20000.0,0.20},
{20000.0,40000.0,0.25},
{40000.0,60000.0,0.30},
{60000.0,80000.0,0.35},
{80000.0,100000.0,0.40},
{100000.0,Double.MAX_VALUE,0.45},
}; private double totalSalary;
public Tax(double totalMoney){
this.totalSalary=totalMoney;
} public double getSalary(){
return totalSalary;
}
public double getTax(){
return getLowTax(getTopTax())+getDibs();
} private int getTopTax(){
if(totalSalary-LOWSALARY>0){
for(int i=TAX.length-1;i>=0;i--){
if(totalSalary-LOWSALARY-TAX[i][1]>0){
return i;
}
}
}
return -1;
}
private double getDibs(){
if(getTopTax()!=-1){
return (totalSalary-LOWSALARY-TAX[getTopTax()+1][0])*TAX[getTopTax()+1][2];
}
return 0.0;
}
private double getLowTax(int startIndex){
int lowTax=0;
if(startIndex!=-1){
for(int i=startIndex;i>=0;i--){
lowTax += (TAX[i][1]-TAX[i][0])*TAX[i][2];
}
}
return lowTax;
} public static void main(String[] args)
{
Tax tax=new Tax(8000);
System.out.println(tax.getTax());
}
}
public class Tax
{
public static final double LOWSALARY=800;
public static final double[][] TAX={
{0,500,0.05},
{500,2000,0.10},
{2000,5000,0.15},
{5000,20000,0.20},
{20000,40000,0.25},
{40000,60000,0.30},
{60000,80000,0.35},
{80000,100000,0.40},
{100000,Double.MAX_VALUE,0.45},
};
private double totalSalary=-1;
private int startIndex=-1;
public Tax(double totalMoney){
this.totalSalary=totalMoney;
}
public double getSalary(){
return totalSalary;
}
public double getTax(){
return getLowTax(getStartIndex())+getDibs();
}
private int getStartIndex(){
if(startIndex==-1){
if(totalSalary-LOWSALARY>0){
for(int i=TAX.length-1;i>=0;i--){
if(totalSalary-LOWSALARY-TAX[i][1]>0){
startIndex = i;
break;
}
}
}
}
return startIndex;
}
private double getDibs(){
if(getStartIndex()!=-1){
return (totalSalary-LOWSALARY-TAX[getStartIndex()+1][0])*TAX[getStartIndex()+1][2];
}
return 0;
}
private double getLowTax(int startIndex){
int lowTax=0;
if(startIndex!=-1){
for(int i=startIndex;i>=0;i--){
lowTax += (TAX[i][1]-TAX[i][0])*TAX[i][2];
}
}
return lowTax;
} public static void main(String[] args)
{
Tax tax=new Tax(8000);
System.out.println(tax.getTax());
}
}
----------
怎么你不先show自己的答案?
¼ÆËã¸öÈËËùµÃË°
*/
public class TaxCalculator
{
private static final double RATE[] ={0.05d,0.1d,0.15d,0.2d,0.25d,0.3d,0.35d,0.4d,0.45d};
private static final double SECTION[] = {0d,500d,2000d,5000d,20000d,40000d,60000d,80000d,100000d};
private static final double LOWBOUND = 800d;
public static void main(String[] args)
{
double sectionFullTax[] = new double[9];
sectionFullTax[0] = 0d;
for (int i=1; i<9; i++)
{
sectionFullTax[i] = 0d;
for (int j=1; j<=i; j++)
sectionFullTax[i] += (SECTION[j] - SECTION[j-1]) * RATE[j-1];
}
/*ÆäʵsectionFullTaxÓÉÊý×éRATE¡¢SECTION¾ö¶¨£¬ÊǶ¨Öµ£¬¿ÉÒÔ²»ÓÃÔÚ³ÌÐòÖÐË㣬
ÌáÇ°ËãºÃ£¬ÏóRATE¡¢SECTIONÒ»Ñù×÷Ϊ³£Á¿Êý×é´æ·Å¡£ÏÂÃæ²ÅÊÇÕæÕý¼ÆËãÓ¦½»ËùµÃË°¡£*/
double salary = Double.parseDouble(args[0]) - LOWBOUND;
System.out.println("salary : " + salary);
double tax = 0d;
for (int i=8; i>=0; i--)
{
if ((salary - SECTION[i])>0)
{
tax = (salary - SECTION[i]) * RATE[i] + sectionFullTax[i];
break;
}
}
System.out.println("Tax : " + tax);
}
}
{
private static final double RATE[] ={0.05d,0.1d,0.15d,0.2d,0.25d,0.3d,0.35d,0.4d,0.45d};
private static final double SECTION[] = {0d,500d,2000d,5000d,20000d,40000d,60000d,80000d,100000d};
private static final double LOWBOUND = 800d;
public static void main(String[] args)
{
double sectionFullTax[] = new double[9];
sectionFullTax[0] = 0d;
for (int i=1; i<9; i++)
{
sectionFullTax[i] = 0d;
for (int j=1; j<=i; j++)
sectionFullTax[i] += (SECTION[j] - SECTION[j-1]) * RATE[j-1];
}
/*其实sectionFullTax由数组RATE、SECTION决定,是定值,可以不用在程序中算,
提前算好,象RATE、SECTION一样作为常量数组存放。下面才是真正计算应交所得税。*/
double salary = Double.parseDouble(args[0]) - LOWBOUND;
System.out.println("salary : " + salary);
double tax = 0d;
for (int i=8; i>=0; i--)
{
if ((salary - SECTION[i])>0)
{
tax = (salary - SECTION[i]) * RATE[i] + sectionFullTax[i];
break;
}
}
System.out.println("Tax : " + tax);
}
}