Java方法没有得到正确的价值,

时间:2021-08-28 13:23:06

I was experimenting with a program idea and therefore have way to many classes, which very well could have been methods, however in my CF method (Don't worry about the naming conventions they were changed to post online) however, when I try to set cF to cF=getTi-getTe the values from the getters always print 0 but this is only in the calculate cF method. If I print the getters and setters in the main method alone they print out with the correct numbers.

我正在尝试一个程序的想法,因此有很多类,这很可能是方法,但在我的CF方法(不要担心命名约定,他们被改为在线发布)然而,当我尝试将cF设置为cF = getTi-getTe来自getters的值始终打印0,但这仅在calculate 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());


    }

}

Sample output **Enter RI

样本输出**输入RI

2000

Enter Misc Income

输入杂项收入

0

Total Income is: 2000.0

总收入为:2000.0

Enter Exp1// these are a combination of other things in the actual program 900

输入Exp1 //这些是实际程序900中其他内容的组合

Total Expenses :900.0

总费用:900.0

900 2000

CF 0.0

1 个解决方案

#1


1  

You just make two new objects without setting its value, sure it returns 0.

您只需创建两个新对象而不设置其值,确保它返回0。

Income rI= new Income();

Expenses e = new Expenses();

To make it workable, here are hints:

为了使其可行,这里有一些提示:

//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;
}

#1


1  

You just make two new objects without setting its value, sure it returns 0.

您只需创建两个新对象而不设置其值,确保它返回0。

Income rI= new Income();

Expenses e = new Expenses();

To make it workable, here are hints:

为了使其可行,这里有一些提示:

//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;
}