我一直在試圖將它實現爲for循環。我寫了一個這個程序的流程圖。該程序需要重複,直到n = 1。我包含了一個鏈接到我的流程圖。如果有人能夠幫助我,那就太棒了。我需要幫助實現一個for循環
using System;
namespace collatzconjecture
{
class MainClass
{
public static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
if (n == 1)
{
Console.WriteLine("n = {0}", n);
}
else if (n % 2 == 0)
{
int a = n/2;
Console.WriteLine("n = {0}", a);
}
else
{
int b = 3 * n + 1;
Console.WriteLine("n = {0}", b);
}
Console.ReadKey();
}
}
}
你究竟在掙扎着什麼?你不能獲得比簡單的for循環更多的基礎。 –
你的意思是你想繼續做n/2或3 * n + 1,直到n爲1?如果是這種情況,那麼你真的想要一個'while'循環,而不是'for'循環,或者像流程圖那樣使用遞歸。 – juharr
流程圖不循環。 – Romoku