需要帮助女巫创造方法 [英] Need help witch creating method

查看:75
本文介绍了需要帮助女巫创造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些在线课程,我正在努力学习C#Smile | :)。在我看来,我创造了无限循环。任何肝脏?

Im doing some online course, I'm trying to learn C # Smile | :) . It appears to me that i created infinite loop. Any hepl?

 class Program
    {
        /// <summary>
        /// Calculates the sum of the integers between start and end (including start and end)
        /// [TestMethod]
        /// </summary>
        /// <param name="start">Start number</param>
        /// <param name="end">End number</param>
        /// <returns>Sum of the integers between start and end (including start and end)</returns>
        public static int Sum(int start, int end)
        {
            
            if (start >= end)
            {
                return  0;
            }
            // TODO add your code here
            else
            {
               
                return Sum(start, end);
            }
 
        }
 
        static void Main(string[] args)
        {
            int start, end;
            Console.WriteLine("ukucajte prvi broj");
            start =Convert.ToInt32(Console.ReadLine());
 
            Console.WriteLine("ukucajte drugi broj");
            end = Convert.ToInt32(Console.ReadLine());
 
           
            Console.WriteLine("suma je: " + Sum(start,end));
       
            Console.Read();
        }
    }
}





我尝试了什么:



这是一个错误:

Methods.exe



What I have tried:

this is a error:
An unhandled exception of type 'System.StackOverflowException' occurred in Methods.exe

推荐答案

嗯,是的......

Well, yes...
public static int Sum(int start, int end)
{
    if (start >= end)
    {
        return  0;
    }
    // TODO add your code here
    else
    {
        return Sum(start, end);
    }
}



如果你打电话给Sum,开头少于结束:


If you call Sum with start less than end:

Sum(1, 2);

然后它会看两个价值,看看哪个更大。

这是结束,所以如果测试将失败,它将执行 else 子句...它将相同的值传递给Sum方法,因此它再次执行相同的操作,并再次执行,直到它用完为止堆栈空间和失败!

可能,你打算做的是

Then it will look at the two values, and see which is the greater.
It's end, so the if test will fail, and it will execute the else clause ... which passes the same values to the Sum method so it does teh same thing again, and again, until it runs out of stack space and fails!
Probably, what you meant to do was

return start + Sum(start + 1, end);

但说实话,这样做的递归方法有点矫枉过正! :笑:

为什么不尝试使用简单的进行循环?

But to be honest, a recursive method to do this is a bit overkill! :laugh:
Why not try it with a simple for loop?


尝试使用简单的for循环



try using simple for loop

public static int Sum(int start, int end)
   {

       if (start >= end)
       {
           return 0;
       }
       else
       {
           int value = 0;
           for (int i = start; i <= end; i++)
               value += i;

           return value;
       }

   }





使用递归 [ ^ ]





using recursion[^]

public static int Sum(int start, int end)
    {

        if (start > end)
        {
            return 0;
        }
        else
        { 
            return  start  + Sum(start+1 , end);
        }

    }


这篇关于需要帮助女巫创造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆