【问题标题】:converting while loop to recursion将while循环转换为递归
【发布时间】:2020-09-05 23:41:33
【问题描述】:

我在将 while 循环转换为递归时遇到问题...该循环似乎工作正常,但是我尝试了几次将其转换为递归,并且该方法返回的最后一个(返回 c;)为 0 ...我的意思是你怎么能把一个while循环变成一个递归?程序应该计算数组中小于 2 的数字

这是主要的

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));
}

这就是方法

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;
}

【问题讨论】:

  • 向我们展示您的尝试?

标签: java loops recursion


【解决方案1】:

这看起来像一个简单的递归:

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);
}

就叫它GPA(gpa, 1)吧。

您的方法中有很多不必要的比较。看看你对10Lstart 的使用。


例如,假设start = 0。您的任何ifs 都不会进入。最好从 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

【讨论】:

  • 哦,我忘了说如果 start 是负整数或 start 大于 gpa.length 它应该返回 -1 如果 start 等于 gpas.length 它应该返回 0 任何想法?
  • 是的,您可以在特定函数中测试这些特定值,然后如果这些情况都不适用,则调用递归选项来处理其余部分。
  • if(start &lt; 0) return -1, else if(start == gpas.length) return 0, else return GPA(gpas, start).
【解决方案2】:

关于递归函数/方法,最重要的三件事:

  1. 终止条件。
  2. 递归调用方法/函数的值。
  3. 处理参数的位置(递归调用之前/之后)。

按如下方式进行:

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
        }
    }
}

输出:

4

【讨论】:

    【解决方案3】:

    创建递归方法时需要注意的两个重要事项。

    1. 您必须包含一个基本情况,即当 true 停止递归调用并返回值时。如果您没有基本案例,您将遇到 StackOverFlow 异常。

    在您的场景中,当索引值等于数组的长度时,递归将停止。

    1. 方法必须从内部调用自身。

    任何可以迭代的东西也可以成为递归的候选对象。

    递归信息: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);
    
    }
    

    【讨论】:

    • Bradley - This 是一个更好的教程。
    • 谢谢,阿文德。 2个资源比1个好!
    猜你喜欢
    • 2019-05-11
    • 2013-07-20
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    相关资源
    最近更新 更多