public float ComputeTax(float income) //income为工资
成都创新互联公司专注于克山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供克山营销型网站建设,克山网站制作、克山网页设计、克山网站官网定制、微信平台小程序开发服务,打造克山网络公司原创品牌,更为您提供克山网站排名全网营销落地服务。
{
float f_income=income-800; //起征基数为800,同时也可以参数化处理
float[] tax_line={0f,500f,2000f,5000f,20000f,40000f,60000f,80000f,100000f};//计算个人所得税的档次(假设员工为中国国籍,因为国籍不同起征点数不同,税率不同。)
float[] tax_rate={0f,0.05f,0.10f,0.15f,0.20f,0.25f,0.30f,0.35f,0.40f}; //各档次税率
float[] tax_sub={0.f,25f,125f,375f,1375f,3375f,6375f,10375f,15375f}; //采用了简化算法
int i_tax_level=0; //为了计算简便,把工资进行分档
while(f_incometax_line[i_tax_level]i_tax_leveltax_line.length)
{
i_tax_level++;
};
if(i_tax_level0)
{
float f_tax=tax_sub[i_tax_level-1]+(f_income-tax_line[i_tax_level-1])*tax_rate[i_tax_level]; //算税的公式
return f_tax;
}
else
{
return 0f;
}
}
这个题目很有意思。不用这么复杂。大概10几句就做完了。
以前我用C#做过的。先把逻辑简化了。几个IF THEN就搞定
可能你会觉得我在把问题变得复杂化,但是请看下去:
(请将代码中的````换成一个tab符)
首先我们定义一个异常类: UnExpectedNumberException
表示所接收到的数字是我们不想要的,类如,我们只对正数进行平方根处理,
但是传入的是一个负数,于是我们可以抛出这个异常
// UnExpectedNumberException.java
//package cn.plause.test.narcissus;
public final class UnExpectedNumberException extends Exception {
````private static final long serialVersionUID = 3080196113722623423L;
````private static final String MESSAGE_FMT = "Number should bigger than %s and smaller than %s";
````public UnExpectedNumberException(int min, int max) {
````````super(String.format(MESSAGE_FMT, min, max));
````}
}
接下来,我们再定义一个接口:NumberAcceptor (你可以理解为数字接受器)
该接口只有一个方法:
public boolean accept(Integer number) /* throws UnExpectedNumberException */;
该方法接受一个参数Integer number,如果实现该接口的类认为
这个number是他需要的或者相信的或者什么其他的东西的话就会返回true,否则false.
// NumberAcceptor.java
//package cn.plause.test.narcissus;
public interface NumberAcceptor {
````public boolean accept(Integer number) /* throws UnExpectedNumberException */;
}
这个接口具有通用性,今天你可能使用它判断一个数是否是水仙花数,明天说不定会用它
来判断一个数是不是镜像数(3443就是一个镜像数)或者什么其它的数字。
下面我们回到重点,因为前面跟水仙花数的逻辑判断一点也没有搭上边。
我们知道,按照一般的逻辑就是这样
for (i = 100; i 1000; i++) {
````如果 i 是一个水仙花数则打印 i
}
而按照一般的判断一个水仙花数的逻辑就是这样的
i / 100 得到百位,
i % 100 / 10 得到十位,
i % 10 得到个位。
然后把这百、十、个位分别立方相加得到一个和,
再将这个和同 i 相比较,如果相等则是水仙花,否则不是。
其实我们还有另一种逻辑的:
一个数 153,如果把它看成是一个字符串的话就是 "153",
字符串的第一个字符是 '1',第二个是 '5',第三个是 '3'。
为什么不直接 1 * 3 + 5 * 3 + 3 * 3 呢?
可能你会说 '1' != 1,
那么 '1' - '0' 呢?!
'1' 的ASC码是 31,'0'的ASC码是30,
显示有 '1' - '0' = 1, '8' - '0' = 8
所以我们可以直接 ('1' - '0') * 3 + ('5' - '0') * 3 + ('3' - '0') * 3
注意,本人并不是想说明别人的算法是不正确的,只是想说清楚我的算法。
请看最重要的类:
(怕大家调试不方便,所以把异常去掉了)
// NarcissusNumber.java
//package cn.plause.test.narcissus;
public final class NarcissusNumber {
````/**
```` * 计算一个数字的每一位的立方和。
```` * 这个方法不只是能算出 3 位数的水仙花数。
```` */
````public static int pow(Integer number) {
````````String digits = number.toString();
````````int total = 0;
````````/*
```````` * 字符 '9' - '0' = 9, '8' - '0' = 8
```````` * 以此类推……
```````` */
````````for (int i = 0; i digits.length(); i++) {
````````````total += (int) Math.pow(digits.charAt(i) - '0', 3);
````````}
````````return total;
````}
````public static void main(String[] args) {
````````/* 新建一个数字接受器对象 */
````````NumberAcceptor narcissusNA = new NumberAcceptor() {
````````````/* 实现NumberAcceptor接口中的accept方法 */
````````````public boolean accept(Integer number) /* throws UnExpectedNumberException */ {
````````````````// 三位数的水仙花数肯定是大于100小于1000的数
````````````````if (! (number 100 number 1000)) {
````````````````````/* throw new UnExpectedNumberException(100, 1000); */
````````````````}
````````````````// 如果这个整数的每一位的立方和与其本身相等,则其是水仙花数
````````````````return pow(number) == number;
````````````}
````````};
````````// 你可以把这个循环的初始条件与结束条件更改以使本程序算出多位水仙花数
````````for (int i = 101; i 1000; i++) {
````````````// 如果是水仙花数,则打印这个整数
````````````if (narcissusNA.accept(i)) {
````````````````System.out.println(i);
````````````}
````````}
````}
}
希望不要嫌麻烦。
第二个: 个人所得税,这个没有什么扩展性,所以简单写了
public final class TaxUtils {
````public static double calculatePersonalTax(int wage) {
````````if (wage = 1000) { // 没有超过 1000 不用给钱
````````````return 0d;
````````}
````````int diff = wage - 1000;
````````// 不知道超过40000的怎么算
````````if (diff 40000) {
````````````return -1d;
````````}
````````if (diff 20000) { // 超出 20000
````````````return diff * 0.25d;
````````} else if (diff 5000) { // 超出 5000
````````````return diff * 0.20d;
````````} else if (diff 2000) { // 超出 2000
````````````return diff * 0.15d;
````````} else if (diff 500) { // 超出 500
````````````return diff * 0.10d;
````````} else { // 超出小于 500
````````````return diff * 0.05d;
````````}
````}
````public static void main(String[] args) {
````````System.out.println(calculatePersonalTax(30000));
````}
}
java计算个税例子:
/**
* @author Kun Sun
* @Date: 2013.10.15
*/
public class Employee { // 雇员类
private String ID; // ID
private String name; // 姓名
private int salary; // 工资薪金所得
private int insureHome; // “五险一金”数额
private int deduct; // 扣除数额
Employee(){
}
Employee(String ID,String name){ // 带参数的构造方法
this.ID = ID;
this.name = name;
}
Employee(String ID,String name,int salary,int insureHome,int deduct){ // 带参数的构造方法
this.ID = ID;
this.name = name;
this.salary = salary;
this.insureHome = insureHome;
this.deduct = deduct;
}
public String getID() {
return ID;
}
public String getName() {
return name;
}
public int getSalary() {
return salary;
}
public int getInsureHome() {
return insureHome;
}
public int getDeduct() {
return deduct;
}
public void setID(String iD) {
ID = iD;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void setInsureHome(int insureHome) {
this.insureHome = insureHome;
}
public void setDeduct(int deduct) {
this.deduct = deduct;
}
public void selfValue(){ // 个人所得税具体计算
double sefValue;
if(salary=0 salary1500){
sefValue = (double)(salary-insureHome-deduct)*0.03 - 0;
}else if(salary=1500 salary4500){
sefValue = (double)(salary-insureHome-deduct)*0.1 - 105;
}else if(salary=4500 salary9000){
sefValue = (double)(salary-insureHome-deduct)*0.2 - 555;
}else if(salary=9000 salary35000){
sefValue = (double)(salary-insureHome-deduct)*0.25 - 1005;
}else if(salary=35000 salary55000){
sefValue = (double)(salary-insureHome-deduct)*0.30 - 2755;
}else if(salary=55000 salary80000){
sefValue = (double)(salary-insureHome-deduct)*0.35 - 5505;
}else{
sefValue = (double)(salary-insureHome-deduct)*0.45 - 13505;
}
System.out.println(sefValue);
}
}
// 用于测试雇员类
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("第一种调用方法:");
Employee emp = new Employee("1001","Sun");
emp.setSalary(12345);
emp.setInsureHome(890);
emp.setDeduct(55);
System.out.println("编号为"+emp.getID()+",姓名是"+emp.getName()+" 的应纳税额是:");
emp.selfValue();
System.out.println("------------------------\n第二种调用方法:");
Employee emp2 = new Employee("1001","Sun",12345,890,55);
System.out.println("编号为"+emp2.getID()+",姓名是"+emp2.getName()+" 的应纳税额是:");
emp2.selfValue();
System.out.println("------------------------\n第二种调用方法:");
Employee emp3 = new Employee();
emp3.setID("1001");
emp3.setName("Sun");
emp3.setSalary(12345);
emp3.setInsureHome(890);
emp3.setDeduct(55);
System.out.println("编号为"+emp3.getID()+",姓名是"+emp3.getName()+" 的应纳税额是:");
emp3.selfValue();
}
}
运行结果:
public static void main(String[] args) {
System.out.println("输入你的收入:");
Scanner input = new Scanner(System.in);
double money = input.nextFloat();
double tax = 0;
if(money = 500){
tax = money * 0.05;
}else if(money 500 money = 20000){
tax = money * 0.1;
}
// .....
System.out.println("纳税:" + tax + " 元");
}