我正在尝试一个程序的想法,因此可以使用很多类,这很可能是方法,但是在我的CF方法中(不要担心它们被更改为在线发布的命名约定)但是,当我尝试将cF
设置为cF=getTi-getTe
来自getter的值始终打印0
,但这只是在计算cF方法中。如果我单独使用主方法打印getter和setter,则会打印出正确的数字。
public class CF {
private double cF;
Income rI= new Income();
Expenses e = new Expenses();
public double getCF() {
return cF;
}
public void setCF(double cF) {
this.cF = cF;
}
public CF(){
//cF=0;
}
public double calculateCF(){
cF= rI.getTi()-e.getTe();// this line will return 0 no matter what
return cF;
}
}
public class Expenses {
private double tExpenses,exp1,exp2;
public Expenses(){
tExpenses=exp1=exp2=0;
}
public double calculateTotalExpenses(){
tExpenses=exp1+exp2;
return tExpenses;
}
/**
* @return the tExpenses
*/
public double getTe() {// again don't worry about naming conventions //they are correct in the IDE
return tExpenses;
}
/**
* @param tExpenses the tExpenses to set
*/
public void setTe(double tExpenses) {
this.tExpenses = tExpenses;
}
}
public class RI {
private double rI;
private double incomeMisc;
private double tIncome;
public RI(){
rI=0;
incomeMisc=0;
tIncome=0;
}
public double calculateRI(){
tIncome= rI+incomeMisc;
return tIncome;
}
/**
* @return the incomeMisc
*/
public double getIncomeMisc() {
return incomeMisc;
}
/**
* @param incomeMisc the incomeMisc to set
*/
public void setIncomeMisc(double incomeMisc) {
this.incomeMisc = incomeMisc;
}
/**
* @return the tIncome
*/
public double gettIncome() {
return tIncome;
}
/**
* @param tIncome the tIncome to set
*/
public void settIncome(double tIncome) {
this.tIncome = tIncome;
}
}
public class FSAProgram {
public static void main(String[] args) {
RI rI = new RI();
Expenses e = new Expenses();
CF cF= new CF();
System.out.println("Enter RI");
Scanner k = new Scanner(System.in);
rI.I(k.nextDouble());
System.out.println("Enter Misc Income");
rI.setIncomeMisc(k.nextDouble());
rI.calculateRI();
System.out.println("Total Income is: "+ rI.getTi());
System.out.println("Enter expense");
e.setExp1(k.nextDouble());// I know this isn't correct its because // I changed the name to post this question
e.calculateTotalExpenses();
System.out.println("Total Expenses :"+ e.gettExpenses());
//cF.calculateCF();
System.out.println(""+(e.gettExpenses() +" "+ rI.gettIncome()));
// Prints fine when last statement is executed
//The next statement is what returns 0 unfortunately
System.out.println("CF: "+ cF.calculateCF());
}
}
示例输出 **输入RI
2000
输入其他收入
0
总收入为:2000.0
输入Exp1 //这些是实际程序中其他内容的组合 900
总费用:900.0
900 2000
CF 0.0
答案 0 :(得分:1)
您只需创建两个新对象而不设置其值,确保它返回0.
Income rI= new Income();
Expenses e = new Expenses();
为了使其可行,这里有提示:
//in your main
CF cF= new CF(rI, e);
//in your cf
private double cF;
private Income income;
private Expenses expense;
public CF(Income rI, Expense e){
this.income = rI;
this.expense = e;
}