【问题标题】:The output displays zero instead of 23 (correct output) from nowhere输出无处显示零而不是 23(正确输出)
【发布时间】: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;
    }
}

【问题讨论】:

  • 您应该发布更多代码以便我们提供帮助。我们应该能够看到年龄对象的实例化......
  • 你打电话给setSalarysetSavings吗?
  • 由于您得到 0,这意味着您没有设置类实例变量(如建议的上行线程),因为它们在实例化时由 VM 初始化为 0。
  • 我已经设置了 setSalary 和 setSavings 但仍然无法正常工作:(
  • 现在我收到错误消息Cannot make a static reference to the non-static method getAge() from the type age for System.out.println(age.getAge());

标签: java output


【解决方案1】:

我想你不明白它是如何工作的。 其实你的Age age = new Age(); 在哪里? 这将调用您的 public age(),但当时 salarysavings 未设置为任何值,因为您从未设置它们或使用这些参数创建另一个构造函数。

最后你要求getAge() 的值,它不会再次计算它,只会得到错误的计算值。

因此,创建一个带有 2 个参数的构造函数:salarysaving,然后从另一个构造函数添加您的代码,这将正常更改您的输出;)

实际上我认为创建一个计算年龄的方法而不是将其放入构造函数中会更好。

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

将您的年龄等级更改为 Age(并将文件重命名为 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());

【讨论】:

  • 那我还需要加Age age = new Age()吗?
  • 如果我使用方法,方法类型将是静态的,因为salarysaving在方法内部,我还必须将它们设置为静态,因此我不能再有二传手。
【解决方案2】:

你没有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) &lt; savings (=0.0D)falseb (=0.0D) &gt; salary (=0.0D) 也是false

因此永远不会设置age

【讨论】:

  • 我还有什么需要设置的吗?因为它仍然无法正常工作:(
  • 我认为工资金额应该取决于我的输入?
猜你喜欢
  • 2018-01-16
  • 2013-02-25
  • 2019-01-05
  • 2020-09-10
  • 2021-01-11
  • 2019-03-31
  • 2018-11-15
  • 2018-01-17
  • 2013-05-15
相关资源
最近更新 更多