【问题标题】:JAVA Recursion and Iteration Methods [closed]JAVA递归和迭代方法
【发布时间】:2013-03-21 18:07:11
【问题描述】:

您好,我有一个关于我的代码的快速问题,我需要知道错误在哪里以及为什么它没有正确显示递归和迭代方法(这会返回 stackoverflow 错误)。谢谢

import java.util.Scanner;

public class Progression {
    public static int geometricRecursive(int x){
        if(x  == 1)
            return 1;
        else {
            return x * geometricRecursive(x - 1);
        } 
    }

    public static int harmonicRecursive(int x){
        if(x == 1)
            return 1;
        else {
            return x * harmonicRecursive(1/(x - 1));
        }
    }

    public static int geometricIterative(int num){
        int result = 0;
        if(result == 1)
            return result;
        else
            for(int i = 2; i < num; i++){
                result = (i * (i + 1));
            }
        return result;
    }

    public static int harmonicIterative(int num){
        int result = 0;
        if (result == 1)
            return result;
        else
            for (int i = 2; i < num; i ++){
                result = (i * (1/(i+1)));
            }
        return result;
    }

    public static void main(String [] args)
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.println("This program will calculate the geometric and ");
        System.out.println("harmonic progression for the number you enter.");
        System.out.print("Enter an integer that is greater than or equal to 1: ");
        int input = keyboard.nextInt();
        int geomAnswer = geometricRecursive (input);
        double harmAnswer = harmonicRecursive (input);
        System.out.println("Using recursion:");
        System.out.println("The geometric progression of " + input + " is " + geomAnswer);
        System.out.println("The harmonic progression of " + input + " is " + harmAnswer);
        geomAnswer = geometricIterative (input);
        harmAnswer = harmonicIterative (input);
        System.out.println("Using iteration:");
        System.out.println("The geometric progression of " + input + " is " + geomAnswer);
        System.out.println("The harmonic progression of " + input + " is " + harmAnswer);
    }
}

【问题讨论】:

  • 堆栈跟踪?您在调试方面尝试过什么?
  • 到底是什么问题?我的意思是你得到什么是你不想要/不期望的?
  • 你知道整数类型在除法方面的局限性吗?
  • 另外,对于带有“谐波”的函数,你应该使用double,而不是int
  • 是我还是harmonicRecursive永远不会终止?

标签: java recursion iteration


【解决方案1】:

你得到一个 StackOverflow,因为你没有正确执行除法。您正在进行整数除法,这会产生错误的结果。这会导致在您的递归算法中无法达到您的基本条件。

要修复它,请使用双精度并更改基本条件:

public static double harmonicRecursive(double x)
{
   if(x <= 1.0)
   {
       return 1.0;
   }
   else
   {
       return x * harmonicRecursive(1.0 / (x - 1.0));
   }
}

【讨论】:

    【解决方案2】:

    有问题的行是harmonicRecursive(int x) 方法中的return x * harmonicRecursive(1/(x - 1));。代码的1/(x - 1) 部分将为所有x &gt; 2 返回0。您的 if 语句仅检查 x == 1 如果您在该函数中输入大于 2 的任何值,则会出现问题。

    【讨论】:

      【解决方案3】:

      这样改这个方法:

      public static int harmonicRecursive(int x){
      
         if(x <= 1) //Here
      
             return 1;
      
         else {
      
            return x * harmonicRecursive(1/(x - 1));
      
         }
      }
      

      【讨论】:

      • 还将输入更改为double
      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 2021-07-08
      • 2015-04-20
      • 2018-03-10
      • 1970-01-01
      • 2016-07-21
      • 2012-08-29
      • 1970-01-01
      相关资源
      最近更新 更多