我想做的是让程序运行第一组数字。然后使用输出作为“X”。所以看起来像这样 Tnew = 告知 - m(告知 - Tair) 告知的是最后一个方程 Tnew。
ex. new = old +6
old = 7
new = 13
new = 13 +6
repeat
这是代码; 封装热损失;
/**
*
* @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);
}
}
好的,我将代码更改为如下所示:
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;
}
}
但是我在行中遇到错误
x = heatloss(x,m,a);
“分配的值从未被使用过。” 我不太确定这意味着什么?程序中明显使用了X。
最佳答案
为了使递归方法的一次调用能够访问另一个递归方法内部的数据(例如,存储在局部变量中的数据),您需要以某种方式将该数据从第二个递归调用传递到第一个递归调用。在这种情况下,您可能需要考虑让 heatloss
方法返回 heatloss
局部变量的值。例如:
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 */
}
}
通过填写适当的空白(我不确定你在这里如何做到这一点,因为我不熟悉你正在解决的特定问题),你应该能够从更深层的角度传达信息向上级调用。
希望这有帮助!
关于java - Java程序中的递归需要重复最后一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9577581/