简体   繁体   中英

Recursion in Java program needs to repeat last number

What im trying to do is have the program to run the first set of numbers. Then Use output as 'X'. So it looks like this Tnew = Told - m(Told - Tair) Told is the last equations Tnew.

ex. new = old +6
old = 7
new = 13
new = 13 +6
repeat 

Heres the code ; package heatloss;

/**
 *
 * @author Eric Franzen
 */
public class HeatLoss {

    public static void heatloss(double x, double m, double a) {
        double heatloss = x - m * (x - a);

        if (x < 55) {
            System.out.println("potatoe too cold");
        }
        else {

            System.out.println(heatloss);
            heatloss(x,m,a);
            x = heatloss;
        }
    }


    public static void main(String[] args) {

        heatloss(80, .01515 ,25); 

    }  

}

Okay so I changed the code to look like this:

public static double heatloss(double x, double m, double a) {
    double heatloss = x - m * (x - a);

    if (x < 55) {
        System.out.println("potatoe too cold");
        return heatloss;
    }
    else {
        System.out.println(heatloss);
        x = heatloss(x,m,a);
        return heatloss;
    }
}

But I get an error at line

x = heatloss(x,m,a);

That "the Assigned value is never used." Im not really sure what that means? X is clearly used in the program.

In order for one invocation of a recursive method to access data inside of another (for example, data stored in local variables), you need to somehow communicate that data out of the second recursive call into the first. In this case, you might want to consider having the heatloss method return the value of the heatloss local variable. For example:

public static double heatloss(double x, double m, double a) {
    double heatloss = x - m * (x - a);

    if (x < 55) {
        System.out.println("potatoe too cold");
        return /* put something here */
    }
    else {
        System.out.println(heatloss);
        x = heatloss(x,m,a);
        return /* put something here */
    }
}

By filling in the appropriate blanks (I'm not sure how you'd do that here, since I'm not familiar with the particular problem you're solving), you should be able to communicate information out of the deeper calls into the higher-up calls.

Hope this helps!

/**
 *
 * @author Eric Franzen
 */
public class HeatLoss {

    public static void heatloss(double x, double m, double a) {
        double heatloss = x - m * (x - a);

        if (x < 55) {
            System.out.println("potatoe too cold");
        }
        else {

            System.out.println(heatloss);
            x = heatloss;
            heatloss(x,m,a);
        }
    }


    public static void main(String[] args) {

        heatloss(80, .01515 ,25); 

    }  
}
        heatloss(x,m,a);
        x = heatloss;

You're making the mistake of believing that heatloss the method and heatloss the variable are somehow related. They are totally unrelated. When heatloss the method returns its value you must assign that returned value to something (if indeed you intend to make use of the value).

You should "play computer", and step through, with pencil and paper, the operations that would occur in running your program. This will give you a better understanding of what's going on. It's not magic -- it's just a simple "imperative" language where each step uses the results of what's happened in previous (in time) steps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM