简体   繁体   English

将while循环转换为递归

[英]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:关于递归函数/方法,有三个最重要的事情:

  1. The terminating condition.终止条件。
  2. The value with which the method/function is called recursively.递归调用方法/函数的值。
  3. Where (before/after the recursive call) to process the parameter(s).在哪里(递归调用之前/之后)处理参数。

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.创建递归方法时要注意两件重要的事情。

  1. You must include a base case, that when true stops the recursive calls and returns the values.您必须包含一个基本情况,即当 true 停止递归调用并返回值时。 If you don't have a base case, you'll run into a StackOverFlow exception.如果您没有基本案例,您将遇到 StackOverFlow 异常。

In your scenario the recursion will stop when the index value is equal to the length of the array.在您的场景中,当索引值等于数组的长度时,递归将停止。

  1. The method must call itself from within.该方法必须从内部调用自身。

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.

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