[英]converting while loop to recursion
I am having a problem with converting a while loop to a recursion... the loop seems to work fine however I tried several times to turn it to recursion and what the method returns is the last (return c;) as 0... I mean how can you actually turn a while loop into a recursion?我在将while循环转换为递归时遇到问题...循环似乎工作正常但是我尝试了几次将其转换为递归并且该方法返回的是最后一个(返回c;)为0...我的意思是你怎么能把一个while循环变成一个递归? the program should count the numbers below 2 in the array
程序应该计算数组中小于 2 的数字
thats the main这是主要的
public static void main(String[] args) {
double[] gpa = new double[]{2.5, 1.3, 1.3, 3.3, 1.2, 3.2, 4, 2.3, 3.1, 1.2};
int start = 0;
countGPA(gpa,start);
System.out.println(countGPA(gpa, start));
}
and thats the method这就是方法
public static int countGPA(double[] gpas, int start) {
int L = gpas.length;
int c = 0;
int countGPA = c;
while (start < 10) {
if (start > 0 && start != L) {
if (gpas[start] < 2.0 && gpas[start] > 0) {
c++;
}
} else if (start == L) {
return 0;
} else if (start < 0 && start > L) {
return -1;
}
start++;
}
return c;
}
This looks like a simple recursion:这看起来像一个简单的递归:
public int GPA(double[] gpas, int index){
if(index >= gpas.length) return 0;
if(0 < gpas[index] && gpas[index] < 2) return 1 + GPA(gpas, index + 1);
else return GPA(gpas, index + 1);
}
Just call it GPA(gpa, 1)
.只需将其称为
GPA(gpa, 1)
。
There is a lot of unnecessary comparisons in your method.您的方法中有很多不必要的比较。 Look at your uses of
10
, L
and start
.看看你对
10
, L
的使用,然后start
。
For example, suppose start = 0
.例如,假设
start = 0
。 No one of your if
s will enter.您的任何一个
if
都不会进入。 Better to start with 1. Look:最好从 1 开始。看:
if (start > 0 && start != L) //start is 0 so this won't enter
else if (start == L) //start is 0 so this won't enter
else if (start < 0 && start > L) //start is 0 so this won't enter
There are three most important things about a recursive function/method:关于递归函数/方法,有三个最重要的事情:
Do it as follows:执行以下操作:
public class Main {
public static void main(String[] args) {
double[] gpa = new double[] { 2.5, 1.3, 1.3, 3.3, 1.2, 3.2, 4, 2.3, 3.1, 1.2 };
int start = 0;
System.out.println(countGPA(gpa, start));
}
public static int countGPA(double[] gpas, int start) {
return countGPA(gpas, start, 0);
}
public static int countGPA(double[] gpas, int start, int count) {
if (start >= gpas.length) {// Terminating condition
return count;
}
if (gpas[start] < 2.0 && gpas[start] > 0) {
return countGPA(gpas, ++start, ++count);// The recursive call
} else {
return countGPA(gpas, ++start, count);// The recursive call
}
}
}
Output: Output:
4
Two important things to note when creating recursive methods.创建递归方法时要注意两件重要的事情。
In your scenario the recursion will stop when the index value is equal to the length of the array.在您的场景中,当索引值等于数组的长度时,递归将停止。
Anything that can be iterated over can also be a candidate for recursion.任何可以迭代的东西也可以成为递归的候选者。
Info on recursion: https://www.javatpoint.com/recursion-in-java递归信息: https://www.javatpoint.com/recursion-in-java
public int numbersBelowTwo(double[] gpas, int index){
//Base case, when this statement equates to true
//the recursions stops and returns the values.
if (index == gpas.length) return 0;
return gpas[index] < 2 ? 1 + numbersBelowTwo(gpas, ++ index) : numbersBelowTwo(gpas, ++index);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.