首页 > 解决方案 > 为什么 void Main 在 if 条件之后不启动?

问题描述

Void Main在 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

标签: c#

解决方案


欢迎来到堆栈溢出。你不是疯了,只是错过了一段关于编程和控制流的信息。具体来说,迭代语句。

您编写的程序将随时退出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');
}

推荐阅读