[英]The output displays zero instead of 23 (correct output) from nowhere
我的代码用于计算CPF,由于CPF是根据年龄水平计算的,因此我现在正在研究该人的年龄。 因为对于公积金来说,对于35岁以下的人,他们的公积金相当于“ 12 *(0.16 + 0.20)*薪水”,并且我认为人们从22岁开始工作,因为他们只能在开始时使用公积金工作。 由于该人当前的月薪为2000美元,而他仅获得10,000美元的储蓄(包括公积金),因此,经过计算,我认为他只工作了一年甚至更多。 因此,无论我如何修改代码,我的编码都应该显示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;
}
}
我认为您不了解其运作方式。 实际上,您的Age age = new Age();
? 这将调用您的public age()
但那时salary
或savings
没有设置为任何值,因为您从未设置它们或使用这些参数创建另一个构造函数。
最后,您通过getAge()
要求该值,该值将不再进行计算,而只是获得错误的计算值。
因此,创建一个具有两个参数的构造函数: salary
和saving
,然后从另一个构造函数添加您的代码,这将正常更改您的输出;)
实际上,我认为最好创建一种计算年龄的方法,而不是将其放入构造函数中。
-----更新-----
将您的年龄类别更改为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());
您没有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
。
发生这种情况是因为在调用构造函数时, salary
为0.0d
。 0
与任何东西的乘积始终为0
。 最后,检查a (=0.0D) < savings (=0.0D)
为false
, b (=0.0D) > salary (=0.0D)
也为false
。
因此age
是永远不会设定的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.