输出从无处显示零而不是23(正确输出)

时间:2014-11-20 16:19:30

标签: java output

我的代码用于计算公积金,现在我正在努力获取此人的年龄,因为公积金计算是基于年龄水平。因为对于中央公积金,对于年龄小于35岁的人,他们的公积金相当于'12 *(0.16 + 0.20)*工资',我假设人们开始工作在22岁,因为他们只能在他们开始时使用公积金工作。由于这个人目前的月薪是2000美元,而他只有1万美元的储蓄(包括公积金),所以经过计算,我认为他只开始工作一年还有一些。   因此,我的编码应显示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;
    }
}

2 个答案:

答案 0 :(得分:2)

我认为你并不了解它是如何运作的。 实际上你的Age age = new Age();在哪里? 这会调用您的public age(),但当时salarysavings未设置为任何值,因为您从未设置它们或使用这些参数创建另一个构造函数。

最后你要求getAge()的值,它不会再次计算它,但只是得到了不好的计算值。

所以创建一个包含2个参数的构造函数:salarysaving,然后从其他构造函数中添加代码,这将正常更改输出;)

实际上在我看来,创建一个计算年龄的方法会更好,而不是把它放在构造函数中。

-----更新-----

将您的年龄段更改为年龄(并将文件重命名为Age.java)

public class Age 
{
    private int age = -1;
    private double salary;
    private double savings;

public Age() {
}
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 int getAge() {
    computeAge();
    return age;
}

public void computeAge()
{
    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.");
}

}

但是,对于一个班级来说,年龄是一个非常奇怪的名字,因为它并不是真正的...

然后调用它:

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("");

Age a = new Age( c.getSalary(), c.getSavings()); // ADD this line

System.out.println("OUTPUT VALUES");
System.out.println("*************");
System.out.println(age.getAge());

答案 1 :(得分:1)

您没有else,因此您的age实际上从未设置过:

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
  age = DEFAULT_AGE;

只需定义DEFAULT_AGE

这是因为在调用构造函数时,salary0.0d0乘以任何内容总是0。最后,您还要检查a (=0.0D) < savings (=0.0D) falseb (=0.0D) > salary (=0.0D)是否false

因此永远不会设置age