刚学习java,有个源程序没明白,请教各位下
本帖最后由 daifengfeng1994 于 2013-12-11 12:13:48 编辑 package nit.wen;
import javax.swing.JOptionPane;
public class one {
public static void main(String[] args) {
String statusString = JOptionPane.showInputDialog("Enter the filing status:");
int status = Integer.parseInt(statusString);
这里是不是调用 public static double computeTax(int status, double income)
String incomeString = JOptionPane.showInputDialog("Enter the taxable income:");
double income = Double.parseDouble(incomeString);
然后这里是不是调用public static double computeTax(double income,int r1, int r2, int r3, int r4, int r5)
JOptionPane.showMessageDialog(null, "Tax is" + (int)(computeTax(status, income) * 100) / 100.0);
}
还有个就是这个(computeTax(status, income)为什么这样表示,这就表示tax,为什么?
public static double computeTax(double income,int r1, int r2, int r3, int r4, int r5){
double tax = 0;
if(income <=r1)
tax = income * 0.10;
else if (income <= r2)
tax = r1*0.10 +(r2 -r1) *0.15 ;
else if (income <= r3)
tax = r1*0.10 +(r2 -r1) *0.15 +(income -r2) * 0.27;
else if (income <= r4)
tax = r1*0.10 +(r2 -r1) *0.15 + (r3 -r2) * 0.27 + (income - r3) * 0.30;
else if (income <= r5)
tax = r1*0.10 +(r2 -r1) *0.15 + (r3 -r2) * 0.27 + (r4 - r3) * 0.30 + (income - r4) * 0.35;
else
tax = r1*0.10 +(r2 -r1) *0.15 + (r3 -r2) * 0.27 + (r4 - r3) * 0.30 + (r5 - r4) * 0.35 +(income - r5) * 0.386;
return tax;
}
public static double computeTax(int status, double income) {
switch (status){
case 0: return
computeTax(income, 6000, 27950, 67700, 141250, 307050);
case 1: return
computeTax(income, 12000, 46700, 112850, 171950, 307050);
case 2: return
computeTax(income, 6000, 23350, 56425, 85975, 153525);
case 3: return
computeTax(income, 10000, 37450, 96700, 15600, 307050);
default: return 0;
}
}
}
[解决办法]
public class one {
public static void main(String[] args) {
String statusString = JOptionPane.showInputDialog("Enter the filing status:");//接受用户输入的内容,返回字符串
int status = Integer.parseInt(statusString);//Integer.parseInt("")将括号中的字符串转换为Integer类型,Integer自动转换为int类型
//至于你这个位置的问题。。。我是在没有找到这个附近调用computeTax的...
String incomeString = JOptionPane.showInputDialog("Enter the taxable income:");
double income = Double.parseDouble(incomeString); //同理,转换为double的
//将内容输出...此处调用computeTax(int status, double income),至于为什么调用下面的那个,看参数个数...java面向对象的重载特性
JOptionPane.showMessageDialog(null, "Tax is" + (int)(computeTax(status, income) * 100) / 100.0);
}
//你的第三个问题,这个方面没有被调用,至于如果调用了,也没有表示tax啊,double tax = 0表示重新创建了一个变量
public static double computeTax(double income,int r1, int r2, int r3, int r4, int r5){
double tax = 0;
....
}
//这里才是被调用的,computeTax(status ,income)两个参数分别对应你输入的内容,在这个方法中表示引用
public static double computeTax(int status, double income) {
switch (status){
case 0: return
computeTax(income, 6000, 27950, 67700, 141250, 307050);
case 1: return
computeTax(income, 12000, 46700, 112850, 171950, 307050);
case 2: return
computeTax(income, 6000, 23350, 56425, 85975, 153525);
case 3: return
computeTax(income, 10000, 37450, 96700, 15600, 307050);
default: return 0;
}
}
}