学Java以来写的第一个函数------税收问题
在我国,个人所得税是基于纳税人的情况和应征收人计算的。纳税人情况共分为三种:单身纳税人、已婚纳税人和家庭纳税人。另外,对于不同情况的纳税人,其税率是分档计算的,我国2008年的个人所得税税率如下表所示。
import java.util.Scanner;
public class Tax2
{
public static double Income(){ //返回值类型为double
double income = 0;
System.out.println("请输入可征税收入");
Scanner in = new Scanner(System.in);
if(in.hasNextDouble()) //in.hasNextDouble判断输入的值是否为double型
income = in.nextDouble(); //是就执行此步骤,不是则跳过
return income;
}
public static void main(String[] args)
{
double income = Income();
int status = 0;
double tax = 0;
System.out.println(“请输入纳税人的类型”);
Scanner inStatus = new Scanner(System.in);
if(inStatus.hasNextInt())
{
status = inStatus.nextInt();
}
if(status == 0)
{
if(income <=6000)
tax = income * 0.10;
else if(income <= 27950)
tax = 6000 * 0.10 + (income - 6000) * 0.15;
else if(income <= 67700)
tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (income - 27950) * 0.27;
else if(income <=141250)
tax = 6000 * 0.10 + ( 27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (income - 67700) * 0.30;
else
tax = 6000 * 0.10 + ( 27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (141250 - 67700) * 0.30 + (income - 141250) * 0.35;
}
else if(status==1){
if(income<=12000)
tax = income * 0.10;
else if(income<=46700)
tax = 12000*0.10 + (income - 6000)*0.15;
else if(income<=112850)
tax = 12000*0.10 + (46700 - 12000)*0.15 + (income - 46700) *0.27;
else if(income<=171950)
tax = 12000*0.10 + (46701 - 12000)*0.15 + (112850 - 46701) *0.27 + (income - 112850)*0.30;
else
tax = 12000*0.10 + (46701 - 12000)*0.15 + (112850 - 46701) *0.27 + (171950 - 112850)*0.30 + (income - 171905)*0.35;
}
else if(status==2){
if(income<=10000)
tax = income * 0.10;
else if(income<=37450)
tax = 10000*0.10 + (income - 10000)*0.15;
else if(income<=96700)
tax = 10000*0.10 + (37450 - 10000)*0.15 + (income - 37450) *0.27;
else if(income<=156600)
tax = 10000*0.10 + (37450 - 10000)*0.15 + (96700 - 37450) * 0.27 + (income - 96700)*0.30;
else
tax = 10000*0.10 + (37450 - 10000)*0.15 + (96700 - 37450) * 0.27 + (156600 - 96700)*0.30 + (income - 156600)*0.35;
}
System.out.println("纳税人需要交税额为" + tax +"¥");
}
}
总结:内容较为简单,只是过程比较繁琐,但还需多加练习才能够熟练掌握自定义函数。