我正在学习C#并且无法理解这个循环。 它显示
“循环8次” “循环9次” “循环9次”
但据我了解,它应该显示出来 “循环8次” “循环9次” “循环10次”
我尝试使用调试工具来帮助我查看,但实际上并不确定在哪里放置断点来帮助我。你能帮我解释一下它为什么是递归的以及它是如何工作的?
namespace LoopTest
{
class Program
{
private static int WriteToConsole(int numberOfWrites)
{
for (int i = numberOfWrites; i < 10; i = WriteToConsole(i + 1))
{
Console.WriteLine("Looped {0} times.", i);
}
return numberOfWrites;
}
static void Main(string[] args)
{
WriteToConsole(8);
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
这是因为你经常调用这个方法,所以对于第一个函数调用if将被迭代为8和9,而递归调用只有一次,所以对于9。
所以它就像那样
WriteToConsole(8)//, it's < 10 so it will print the call with 8 and call the same method with 9
WriteToConsole(9)//, it's < 9 so it will print the call with 9 and **not** call the same method with 10 (as it's not < 10) and return 9 as the result of the function
//Then it returns to the first call, with returned int 9 as i and it will print 9 again as the first call result
我将使用'&lt; ='代替'&lt;'
来解释一个示例WriteToConsole(8) //1st level, prints 8, calls recursively 9
| WriteToConsole(9)//2nd level, prints 9, calls recursively 10
|| WriteToConsole(10)//3rd level, prints 10, calls recursively 11
||| WriteToConsole(11) //4th level, doesn't print anything (>10) and returns 11
|| Loop does not execute as returned i = 11 so > 10, returns 10 (because WriteToConsole(10) was initially called)
| WriteToConsole(10)//2nd level, prints 10, calls recursively 11
|| WriteToConsole(11) //3rd level, doesn't print anything (>10) and returns 11
| Loop does not execute as returned i = 11 so > 10, returns 10 (because WriteToConsole(9) was initially called)
WriteToConsole(9) //1st level, prints 9, calls recursively 10
| WriteToConsole(10) //2nd level, prints 10, calls recursively 11
|| WriteToConsole(11) //3rd level, doesn't print anything (>10) and returns 11
| Loop does not execute as returned i = 11 so > 10, returns 10 (because WriteToConsole(10) was initially called)
WriteToConsole(10) //1st level, prints 10, calls recursively 11
| WriteToConsole(11) //2nd level, doesn't print anything (>10) and returns 11
Loop does not execute as returned i = 11 so > 10, returns 8 (because WriteToConsole(10) was initially called)
为了说明,我已经添加了递归级别参数以及更多日志记录到您的代码中,您可以在我的dotnetfiddle(https://dotnetfiddle.net/het1w1)上看到,这是结果:
Looped 8 times. Recursion level 1
Looped 9 times. Recursion level 2
Looped 10 times. Recursion level 3
returned: 11
returned: 10
Looped 10 times. Recursion level 2
returned: 11
returned: 9
Looped 9 times. Recursion level 1
Looped 10 times. Recursion level 2
returned: 11
returned: 10
Looped 10 times. Recursion level 1
returned: 11
returned: 8
答案 1 :(得分:0)
这是因为你只使用<
(小于)你需要将其更改为<=
(等于或小于),这将包括第10个:
namespace LoopTest
{
class Program
{
private static int WriteToConsole(int numberOfWrites)
{
for (int i = numberOfWrites; i <= 10; i = WriteToConsole(i + 1))
{
Console.WriteLine("Looped {0} times.", i);
}
return numberOfWrites;
}
static void Main(string[] args)
{
WriteToConsole(8);
Console.ReadKey();
}
}
}
答案 2 :(得分:0)
您需要将for循环更改为
number (int) | time_stamp (datetime2) | number2 (int) | time_stamp2 (datetime2)
--------------------------------------------------------
20 | '2017-08-01 01:00:00' | null | null
100 | '2017-08-01 01:00:00' | null | null
答案 3 :(得分:0)
这只是因为你在for循环增量语句中使用了 WriteToConsole
请替换此行
for (int i = numberOfWrites; i <= 10; i = WriteToConsole(i + 1))
带
for (int i = numberOfWrites; i <= 10; i = i + 1)
这样你就可以轻松理解循环。
答案 4 :(得分:0)
当您致电WriteToConsole(8);
时,该方法内的for循环将:
Console.WriteLine("Looped 8 times.");
i
:i = WriteToConsole(i + 1)
的值,此行将再次调用(递归)WriteToConsole(9); 使用numberOfWrites = 9
:Console.WriteLine("Looped 9 times.");
但不要忘记第一种方法(WriteToConsole(8);
)尚未完成。我们在设置i
的值时离开了该方法。
因此,如果您恢复i = 9的for循环:Console.WriteLine("Looped 9 times.");
我认为你错过了for循环中的控制流:https://www.tutorialspoint.com/csharp/csharp_for_loop.htm