为什么在if条件之后Main不会无效?

时间:2019-07-23 05:46:43

标签: c#

if语句后,虚空主线不会再次启动。

我已经尝试了一段时间的这种结构,并且通常它们可以工作。但是事实并非如此。生成器空位在下面的某个地方,请不要考虑它。

class Program
    {
        private static string Input = Console.ReadLine();
        static void Main()
        {

            Console.WriteLine("Press any key to start");
            Program.Generator();
            Console.WriteLine("Are you satisfied? Type '1' if yes or '2' if no.");
            Console.ReadLine();
            if (Input == "2")
            {
                Program.Main();
            }
        }
    }

没有错误,没有错误,控制台在完成Main void之后就关闭了。

1 个答案:

答案 0 :(得分:2)

欢迎堆栈溢出。您不会发疯,只是缺少一些有关编程和控制流的信息。具体来说,iteration statements.

您编写的程序将在Input拥有2以外的任何其他值时退出,因为没有循环迫使它反复执行。

static void Main()
{

    Console.WriteLine("Press any key to start");
    Console.ReadKey();

    do
    {
        // Program.Generator();
        Console.WriteLine("Are you satisfied? Type '1' if yes or '2' if no.");

    } while (Console.ReadKey().KeyChar != '1');
}