试图将循环更改为递归方法。遇到麻烦
Trying to change a loop to a recursive method. Having some toruble
我需要在我的最终项目中加入递归。有人建议我简单地将我的一个循环更改为递归方法。这是我到目前为止所拥有的。每次都返回0值。
public static double GetCostTotal(Liquor[] inv, int index) {
if ( index >= inv.length) return 0;
else return GetCostTotal(inv, index + 1);
}
这是我在主要方法中调用它的地方:
//beer. regular 1d array
System.out.println("\nNow for beer. How many beers are you taking inventory of?: ");
int sizeBeers = keyboard.nextInt();
Beer[] invBeers = new Beer[sizeBeers];
for (int i = 0; i < invBeers.length; i++) {
invBeers [i] = new Beer();
System.out.println("Enter product name: ");
invBeers [i].setLiquorName(keyboard.next());
System.out.println("Enter the count: ");
invBeers [i].setLiquorCount(keyboard.nextDouble());
System.out.println("Enter the cost: ");
invBeers [i].setLiquorCost(keyboard.nextDouble());
}
//calls GetCostTotal method and passes invBeers array
double beerCost = GetCostTotal(invBeers, sizeBeers);
System.out.println("The total cost of the beer inventory is: $" + beerCost);
我仍在努力寻找自己的解决方案,但显然也在寻求你们的帮助。谢谢!
你的递归方法应该是这样的:
public static double GetCostTotal(Liquor[] inv, int index, double total) {
if (index >= inv.length)
return total;
else
total = inv[index].amount() * inv[index].cost();
return GetCostTotal(inv, index + 1, total);
}
可以看到total
是传递给函数的参数,可以有效的累加成本总和。否则,您将在每次调用时覆盖金额。其次,else
子句正在执行必要的计算。
最后,您的初始调用触发了默认(基本)情况,因为您传递的是数组的长度而不是从零开始。
double beerCost = GetCostTotal(invBeers, 0, 0);
由于递归调用将 index
递增 1,因此您需要为 index
传递零,为 total
传递零。
如果你这样做,你应该得到正确的结果。这是我的示例输入和结果:
Now for beer. How many beers are you taking inventory of?:
3
Enter product name:
Bud
Enter the count:
1
Enter the cost:
2
Enter product name:
Miller
Enter the count:
2
Enter the cost:
3
Enter product name:
Michelob
Enter the count:
3
Enter the cost:
4
The total cost of the beer inventory is: .0
您可以看到第一次迭代的累积成本是 2.00 (12),第二次是 6.00 (23),最后一次是 12.00 ( 3*4),总计 20.00 (2 + 6 + 12)
注意:我为 Beer
创建了一个 Java 记录,而不是常规的 class。这就是为什么我在 GetCostTotal
中调用的方法是 cost()
和 amount()
。您需要更改它们以匹配您正确的方法名称,或者将 Liquor
和 Beer
更改为 Java 记录,如果您使用的是最新的 Java,我强烈建议您这样做。
只需将当前商品的总费用与递归调用的结果相加即可:
public static double GetCostTotal(Liquor[] inv, int index) {
if (index >= inv.length) return 0;
else return (inv.getLiquorCount() * inv.getLiquorCost()) + GetCostTotal(inv, index + 1);
}
注意:这不是尾递归,而且,无论如何,Java 不会优化尾调用,所以如果你的数组太长,你会得到一个 WhosebugError。
如果您将其修改为使用累加器(如 hfontanez 的回答),并使用能够正确消除尾调用的语言和平台编写,则它不会溢出。希望有一天 Java 会添加 TCO/TCE.
解决方法在这里。没有执行必要的计算并且错误地调用了方法。
public static double GetCostTotal(Liquor[] inv, int index, double totalCost) {
if ( index >= inv.length) return totalCost;
else totalCost = inv[index].getLiquorCount()*inv[index].getLiquorCost();
return GetCostTotal(inv, index + 1, totalCost);
}
double beerCost = GetCostTotal(invBeers, 0, 0);
System.out.println("The total cost of the beer inventory is: $" + beerCost);
我需要在我的最终项目中加入递归。有人建议我简单地将我的一个循环更改为递归方法。这是我到目前为止所拥有的。每次都返回0值。
public static double GetCostTotal(Liquor[] inv, int index) {
if ( index >= inv.length) return 0;
else return GetCostTotal(inv, index + 1);
}
这是我在主要方法中调用它的地方:
//beer. regular 1d array
System.out.println("\nNow for beer. How many beers are you taking inventory of?: ");
int sizeBeers = keyboard.nextInt();
Beer[] invBeers = new Beer[sizeBeers];
for (int i = 0; i < invBeers.length; i++) {
invBeers [i] = new Beer();
System.out.println("Enter product name: ");
invBeers [i].setLiquorName(keyboard.next());
System.out.println("Enter the count: ");
invBeers [i].setLiquorCount(keyboard.nextDouble());
System.out.println("Enter the cost: ");
invBeers [i].setLiquorCost(keyboard.nextDouble());
}
//calls GetCostTotal method and passes invBeers array
double beerCost = GetCostTotal(invBeers, sizeBeers);
System.out.println("The total cost of the beer inventory is: $" + beerCost);
我仍在努力寻找自己的解决方案,但显然也在寻求你们的帮助。谢谢!
你的递归方法应该是这样的:
public static double GetCostTotal(Liquor[] inv, int index, double total) {
if (index >= inv.length)
return total;
else
total = inv[index].amount() * inv[index].cost();
return GetCostTotal(inv, index + 1, total);
}
可以看到total
是传递给函数的参数,可以有效的累加成本总和。否则,您将在每次调用时覆盖金额。其次,else
子句正在执行必要的计算。
最后,您的初始调用触发了默认(基本)情况,因为您传递的是数组的长度而不是从零开始。
double beerCost = GetCostTotal(invBeers, 0, 0);
由于递归调用将 index
递增 1,因此您需要为 index
传递零,为 total
传递零。
如果你这样做,你应该得到正确的结果。这是我的示例输入和结果:
Now for beer. How many beers are you taking inventory of?:
3
Enter product name:
Bud
Enter the count:
1
Enter the cost:
2
Enter product name:
Miller
Enter the count:
2
Enter the cost:
3
Enter product name:
Michelob
Enter the count:
3
Enter the cost:
4
The total cost of the beer inventory is: .0
您可以看到第一次迭代的累积成本是 2.00 (12),第二次是 6.00 (23),最后一次是 12.00 ( 3*4),总计 20.00 (2 + 6 + 12)
注意:我为 Beer
创建了一个 Java 记录,而不是常规的 class。这就是为什么我在 GetCostTotal
中调用的方法是 cost()
和 amount()
。您需要更改它们以匹配您正确的方法名称,或者将 Liquor
和 Beer
更改为 Java 记录,如果您使用的是最新的 Java,我强烈建议您这样做。
只需将当前商品的总费用与递归调用的结果相加即可:
public static double GetCostTotal(Liquor[] inv, int index) {
if (index >= inv.length) return 0;
else return (inv.getLiquorCount() * inv.getLiquorCost()) + GetCostTotal(inv, index + 1);
}
注意:这不是尾递归,而且,无论如何,Java 不会优化尾调用,所以如果你的数组太长,你会得到一个 WhosebugError。
如果您将其修改为使用累加器(如 hfontanez 的回答),并使用能够正确消除尾调用的语言和平台编写,则它不会溢出。希望有一天 Java 会添加 TCO/TCE.
解决方法在这里。没有执行必要的计算并且错误地调用了方法。
public static double GetCostTotal(Liquor[] inv, int index, double totalCost) {
if ( index >= inv.length) return totalCost;
else totalCost = inv[index].getLiquorCount()*inv[index].getLiquorCost();
return GetCostTotal(inv, index + 1, totalCost);
}
double beerCost = GetCostTotal(invBeers, 0, 0);
System.out.println("The total cost of the beer inventory is: $" + beerCost);