Java For循环成递归函数

发布于 2024-11-05 04:12:18 字数 680 浏览 1 评论 0原文

public class For {
 public static void main(String[] args){
          for(int i=2; i<=1024; i *= 2){
           System.out.println("Count is: " + i);
      }
 }




public class While {
    public static void main(String[] args){
        int i = 1;
        while (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
      }
 }


public class DoWhile {
     public static void main(String[] args){
        int i = 1;
        if (i < 1024) {
            do { i*=2;
                System.out.println("Count is: " + i);
            } while (i < 1024);
        }
     }

如何转换 for 循环/while 循环,使其执行相同的操作,但使用递归函数?

public class For {
 public static void main(String[] args){
          for(int i=2; i<=1024; i *= 2){
           System.out.println("Count is: " + i);
      }
 }




public class While {
    public static void main(String[] args){
        int i = 1;
        while (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
      }
 }


public class DoWhile {
     public static void main(String[] args){
        int i = 1;
        if (i < 1024) {
            do { i*=2;
                System.out.println("Count is: " + i);
            } while (i < 1024);
        }
     }

How would one convert the for loop/while loop so it does the same thing, but using a recursive function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

楠木可依 2024-11-12 04:12:18

就像这样:

public class Recursive {
    public void r(int i) {
        if (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
            r(i);
        }
    }

    public static void main(String[] args) {
        Recursive r = new Recursive();
        r.r(1);
    }
}

Like so:

public class Recursive {
    public void r(int i) {
        if (i < 1024) {
            i *= 2;
            System.out.println("Count is: " + i);
            r(i);
        }
    }

    public static void main(String[] args) {
        Recursive r = new Recursive();
        r.r(1);
    }
}
梦毁影碎の 2024-11-12 04:12:18

获取 main 的循环并将其放入其自己的带有参数 int i 的函数中。在该函数中,将循环重写为

  1. If theloop condition is false (i >= 1024), then return
  2. Else, recursive call with argument i*2

使用参数 12 调用函数,具体取决于您要重写的程序(它们并不完全匹配)。

Take the loop of main and put it in its own function with an argument int i. In that function, rewrite the loop to

  1. If the loop condition is false (i >= 1024), then return
  2. Else, recursive call with argument i*2.

Call the function with argument 1 or 2, depending on which of your programs you're rewriting (they don't entirely match).

心在旅行 2024-11-12 04:12:18

循环循环可以如下所示:

class Main
{
    public static void main(String[] args){
      RecWhile(1);
    }

    public static void RecWhile(int i) {
       if (i < 1024) {
         i = i*2;
         System.out.println("Count is: " + i);
         RecWhile(i);
       }
    }
}

Recurrent loop can look like this:

class Main
{
    public static void main(String[] args){
      RecWhile(1);
    }

    public static void RecWhile(int i) {
       if (i < 1024) {
         i = i*2;
         System.out.println("Count is: " + i);
         RecWhile(i);
       }
    }
}
南汐寒笙箫 2024-11-12 04:12:18

公共类测试1 {

public static void main(String[] args) {

    Test1 mainFunc = new Test1();

    int[] arr = {1,2,4,3,5,6};

    int start=0;
    int end=arr.length;
    mainFunc.callRecursiveFun(start, end, arr);
}

public int callRecursiveFun(int start, int end, int[] arr) {
    int arrLen = end;
    if(arrLen == 0) {
        return 0;
    } else {

    System.out.println("Loop Index at "+start +": "+arr[start]);


    }
    return callRecursiveFun(start+1, end-1, arr);
}

}

public class Test1 {

public static void main(String[] args) {

    Test1 mainFunc = new Test1();

    int[] arr = {1,2,4,3,5,6};

    int start=0;
    int end=arr.length;
    mainFunc.callRecursiveFun(start, end, arr);
}

public int callRecursiveFun(int start, int end, int[] arr) {
    int arrLen = end;
    if(arrLen == 0) {
        return 0;
    } else {

    System.out.println("Loop Index at "+start +": "+arr[start]);


    }
    return callRecursiveFun(start+1, end-1, arr);
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文