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 toGrazintPitaka(4),GrazintPitaka(3), all the way down toGrazintPitaka(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 ownnvalue, 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:
- Main method calls
GrazintPitaka(5):- n isn't 0, so it prepares to return
5 + GrazintPitaka(4)and pauses to run the recursive call.
- n isn't 0, so it prepares to return
GrazintPitaka(4)runs:- Prepares to return
4 + GrazintPitaka(3)and pauses.
- Prepares to return
GrazintPitaka(3)runs:- Prepares to return
3 + GrazintPitaka(2)and pauses.
- Prepares to return
GrazintPitaka(2)runs:- Prepares to return
2 + GrazintPitaka(1)and pauses.
- Prepares to return
GrazintPitaka(1)runs:- Prepares to return
1 + GrazintPitaka(0)and pauses.
- Prepares to return
GrazintPitaka(0)runs:- Hits the termination condition, returns 0.
- Now we backtrack to calculate accumulated results:
GrazintPitaka(1)returns1 + 0 = 1GrazintPitaka(2)returns2 + 1 = 3GrazintPitaka(3)returns3 + 3 = 6GrazintPitaka(4)returns4 + 6 = 10GrazintPitaka(5)returns5 + 10 = 25
- Finally,
Console.WriteLineoutputs 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
nby 1 every time to reach 0.
内容的提问来源于stack exchange,提问作者last of us fan
相关产品推荐
相关产品推荐

