简体   繁体   English

递归使用for循环

[英]Using for loop with recursion

So I am trying to print out the actual process of the recursive exponent method and then the value in the end, but I am having issues with my for loop. 因此,我尝试打印出递归指数方法的实际过程,然后打印出最终的值,但是我的for循环遇到了问题。 For example if the user inputs 4 to the 4th power it should print out 4 to 4, 4 to 3, 4 to 2, etc. and then print out correct value of 4 to 4th power, but I keep getting 0 for the value in the end. 例如,如果用户将4输入4的幂,则应打印4到4、4到3、4到2等,然后打印正确的4到4幂的值,但对于结束。

import java.util.Scanner; 

public class Recursion
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter base: ");
    int baseNum = input.nextInt();

    System.out.println("Enter exponent: ");
    int expNum = input.nextInt();

    for(int i = 1; i <= baseNum; ++i)
    {

    System.out.printf("\n%d to the %d", baseNum, expNum--);

    }

    System.out.printf("\nValue is %d",power(baseNum, expNum));
  }



  public static int power(int base, int exponent)
  {
    if(base == 0)
      return 1;

    else
      return exponent * power(base - 1, exponent);
  }
}

You confused the base with the exponent. 您将基数与指数混淆了。 The power method should be defined like this: 功率方法应定义如下:

public static int power(int base, int exponent) {
    if(exponent == 0) {
        System.out.println(1); // for display
        return 1;
    }
    else {
        int rem = power(base, exponent-1);
        if (rem > 1) {
            System.out.println(rem); // for display
        }
        return base * rem;
    }
}

USAGE 用法

public static void main(String[] args) {
    System.out.println(power(4,2)); // prints 16
}

The recursive definition of exponentiation is 求幂的递归定义为

x^n = x * x^(n-1)

Note that the exponent decreases, not the base. 请注意,指数减少,而不是底数。

Power = base raised to exponent. 功效=基数提高到指数。 You did it the wrong way. 您做错了方法。 Correct way - 正确方法-

public static int power(int base, int exponent) {
    if(exponent == 0)
        return 1;
    else
        return base * power(base, exponent-1);
}

To make the above code display the intermediate results: 要使上面的代码显示中间结果:

import java.util.Scanner;

public class Recursion {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter base: ");
        int baseNum = input.nextInt();

        System.out.println("Enter exponent: ");
        int expNum = input.nextInt();

        int pow = power(baseNum, expNum);

    }

public static int power(int base, int exponent) {

    int pow = 0;

    if (exponent == 0) {
        pow = 1;
        System.out.println(base + " raised to " + exponent + " is " + pow);
    } else {

        pow = base * power(base, exponent - 1);
        System.out.println(base + " raised to " + exponent + " is " + pow);
    }

    return pow;

}

There are two problems with your code 您的代码有两个问题

  1. The exponent is being mutated before you actually use it expNum-- 该指数被突变之前,你实际使用它expNum--
  2. You reverse the base and exponent in power() 您将底数和幂转换为幂()

Below is an edited version of your code that works 以下是可以使用的代码的编辑版本

import java.util.Scanner;

public class Recursion
{

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter base: ");
        int baseNum = input.nextInt();

        System.out.println("Enter exponent: ");
        int expNum = input.nextInt();

        for (int i = 1; i <= expNum; ++i)
        {

            System.out.printf("\n%d to the %d", baseNum, i);

        }

        System.out.printf("\nValue is %d", power(baseNum, expNum));
    }

    public static int power(int base, int exponent)
    {
        if (exponent == 0)
        {
            return 1;
        }

        else
        {
            return base * power(base, exponent - 1);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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