【发布时间】:2014-11-20 16:19:29
【问题描述】:
我的代码用于计算 CPF,现在我正在努力获取此人的年龄,因为 CPF 计算是基于年龄级别的。因为对于公积金,对于35岁以下的人,他们的公积金相当于'12 *(0.16 + 0.20)*薪水',我假设人们从22岁开始工作,因为他们只能在开始时使用公积金在职的。由于这个人目前的月薪是$2000,而他只有$10,000的储蓄(包括CPF),所以经过计算,我认为他才开始工作一年多一点。 因此,我的编码应该显示 23 岁,但无论我如何修改代码,它都显示为 0,我不知道其中哪一部分是错误的,希望有人能帮我弄清楚。谢谢。
这是我的部分输出和输入代码:
public class mainclass {
// The mainclass is an executable class. You should run your program here.
public static void main(String[] args) {
/* cpfCalculator takes in the following
* (String name, double savings, double desiredAmt, double salary, String citizen)
*/
cpfCalculator c = new cpfCalculator("El Salvador", 10000, 70000, 2000, "P", 0.451651);
System.out.println("INPUT VALUES");
System.out.println("*************");
System.out.println("Person's Name:" + c.getName() + "_ Savings ($):" + c.getSavings() + "_ Desired Amount ($):" + c.getDesiredAmt());
System.out.println("Salary ($):" + c.getSalary() + "_ Citizen or PR:" + c.getCitizen());
System.out.println("");
System.out.println("OUTPUT VALUES");
System.out.println("*************");
System.out.println(age.getAge());
System.out.println("Employee CPF ($): " + c.getYcpf());
System.out.println("Employer CPF ($): " + c.getBcpf());
System.out.println("Total CPF per year ($): " + c.getTotalCpf());
System.out.println("No of Years needed to achieve desired amount: " + c.getYearsToAchieve());
System.out.println("Hi, " +c.getName() + ", you should " + c.getAdvice());
System.out.println("The CPF accumulation schedule as follows : ");
System.out.println("Yr. | CPF/yr ($) | Total Amt ($)");
for (int j = 0; j<=c.getCpfAdviceArray().size()-1; j++){
cpfAdvice wa = c.getCpfAdviceArray().get(j);
System.out.println(wa.getYear() + " | " + wa.getCpf() + " | " + (j+1)*wa.getTcpf());
}
这些是用来计算年龄的:
public class age {
private static int age;
private double salary;
private double savings;
public age() {
double a = 12*(0.16 + 0.20) * salary;
double b = 2*a;
if (a < savings)
// only worked for less than one year
age = 22;
else if(a >= savings && b > salary )
// age>22&&age<=35
age = 23;
else
System.out.println("The person hasn't started working.");
}
public age(double salary, double savings) {
super();
this.salary = salary;
this.savings = savings;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setSavings(double savings) {
this.savings = savings;
}
public double getSavings() {
return savings;
}
public static int getAge() {
return age;
}
}
【问题讨论】:
-
您应该发布更多代码以便我们提供帮助。我们应该能够看到年龄对象的实例化......
-
你打电话给
setSalary和setSavings吗? -
由于您得到 0,这意味着您没有设置类实例变量(如建议的上行线程),因为它们在实例化时由 VM 初始化为 0。
-
我已经设置了 setSalary 和 setSavings 但仍然无法正常工作:(
-
现在我收到错误消息
Cannot make a static reference to the non-static method getAge() from the type ageforSystem.out.println(age.getAge());