You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#递归代码返回异常求助:预期25实际返回5

Hey there! Let's break down what's probably going wrong with your recursive method and fix that return value mismatch (expected 25, got 5).

第一步:还原常见的错误代码场景

Since you didn't share your exact code, based on the return value issue, your GrazintPitaka method is likely structured like this (the buggy version):

public class Test
{
    public Test() {} // Empty constructor as mentioned

    public int GrazintPitaka(int n = 5) // Assuming default parameter is 5 for your test
    {
        if (n == 0)
            return 0;
        
        GrazintPitaka(n - 1); // Recursive call made but its result isn't used
        return n;
    }
}

// Main method call
var testInstance = new Test();
Console.WriteLine(testInstance.GrazintPitaka()); // Outputs 5 instead of 25
第二步:Root Cause of the Bug

The problem lies in not incorporating the recursive call's result into the current layer's return value:

  • When you call GrazintPitaka(5), it triggers calls to GrazintPitaka(4), GrazintPitaka(3), all the way down to GrazintPitaka(0) (which returns 0).
  • But each layer only executes the recursive call without adding its result to the current n. In the end, every layer just returns its own n value, so the outermost call returns 5.
第三步:Fixed Code & Step-by-Step Execution Logic

To get the sum 1+2+3+4+5=25, we need to accumulate the recursive call's result with the current n:

public class Test
{
    public Test() {}

    public int GrazintPitaka(int n = 5)
    {
        // Recursion termination condition: stop when n hits 0, return 0 (no more values to add)
        if (n == 0)
            return 0;
        
        // Core logic: current n value + result of recursive call with n-1
        return n + GrazintPitaka(n - 1);
    }
}

Let's walk through how this corrected method runs:

  1. Main method calls GrazintPitaka(5):
    • n isn't 0, so it prepares to return 5 + GrazintPitaka(4) and pauses to run the recursive call.
  2. GrazintPitaka(4) runs:
    • Prepares to return 4 + GrazintPitaka(3) and pauses.
  3. GrazintPitaka(3) runs:
    • Prepares to return 3 + GrazintPitaka(2) and pauses.
  4. GrazintPitaka(2) runs:
    • Prepares to return 2 + GrazintPitaka(1) and pauses.
  5. GrazintPitaka(1) runs:
    • Prepares to return 1 + GrazintPitaka(0) and pauses.
  6. GrazintPitaka(0) runs:
    • Hits the termination condition, returns 0.
  7. Now we backtrack to calculate accumulated results:
    • GrazintPitaka(1) returns 1 + 0 = 1
    • GrazintPitaka(2) returns 2 + 1 = 3
    • GrazintPitaka(3) returns 3 + 3 = 6
    • GrazintPitaka(4) returns 4 + 6 = 10
    • GrazintPitaka(5) returns 5 + 10 = 25
  8. Finally, Console.WriteLine outputs 25, matching your expected result.
Key Recursion Takeaways
  • Always have a clear termination condition: Without it, you'll hit a stack overflow from infinite recursion.
  • Use the recursive call's result in the current layer: Otherwise, recursion just runs nested calls without building up the final value you need.
  • Each recursive step must move closer to the termination condition: Here, we decrement n by 1 every time to reach 0.

内容的提问来源于stack exchange,提问作者last of us fan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:42:36