C# 为什么我的程序在第二次键入内容后关闭

C# 为什么我的程序在第二次键入内容后关闭,c#,console,console-application,C#,Console,Console Application,我这里有这个代码: string code1 = null; Console.Write("Username: " + Environment.UserName.ToString() + ">"); string line = Console.ReadLine(); if (line == "info") { Console.WriteLine("Info:"); } else if (line == "Set Cod

我这里有这个代码:

    string code1 = null;
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
    Console.ReadLine();

当我输入诸如“null”之类的内容时,它将运行else代码。如果我再次尝试输入内容,它会关闭控制台。为什么会这样做?

如果将代码放入while循环,它将不会关闭。以下是编辑后的代码:

string code1 = null;
while(true)
{
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else if (line == "quit")
    {
        break;
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
}

如果将代码放入while循环,它将不会关闭。以下是编辑后的代码:

string code1 = null;
while(true)
{
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else if (line == "quit")
    {
        break;
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
}

如果将代码放入while循环,它将不会关闭。以下是编辑后的代码:

string code1 = null;
while(true)
{
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else if (line == "quit")
    {
        break;
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
}

如果将代码放入while循环,它将不会关闭。以下是编辑后的代码:

string code1 = null;
while(true)
{
    Console.Write("Username: " + Environment.UserName.ToString() + ">");
    string line = Console.ReadLine();
    if (line == "info")
    {
        Console.WriteLine("Info:");
    }
    else if (line == "Set Code")
    {
        if (code1 == null)
        {
            Console.Write("TEST");
        }
        else
        {
            Console.WriteLine("'Set Code' is not known as a command \nEnter 'info' to view all commands");
            Console.Write("Username: " + Environment.UserName.ToString() + ">");
        }
    }
    else if (line == "quit")
    {
        break;
    }
    else
    {
        Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
    }
}
您的第一个
ReadLine()
将如您所注意到的那样等待用户输入。您键入“null”,它将进入

else
{
    Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
}
控制流量

当您到达第二个
ReadLine()
时,您可以键入任何内容,但当您这样做并键入enter时,程序将无事可做,因此退出

如果你想要一个永无止境的输入,你应该尝试做一个循环。

你的第一个
ReadLine()
会像你注意到的那样等待用户输入。您键入“null”,它将进入

else
{
    Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
}
控制流量

当您到达第二个
ReadLine()
时,您可以键入任何内容,但当您这样做并键入enter时,程序将无事可做,因此退出

如果你想要一个永无止境的输入,你应该尝试做一个循环。

你的第一个
ReadLine()
会像你注意到的那样等待用户输入。您键入“null”,它将进入

else
{
    Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
}
控制流量

当您到达第二个
ReadLine()
时,您可以键入任何内容,但当您这样做并键入enter时,程序将无事可做,因此退出

如果你想要一个永无止境的输入,你应该尝试做一个循环。

你的第一个
ReadLine()
会像你注意到的那样等待用户输入。您键入“null”,它将进入

else
{
    Console.WriteLine("'" + line + "' is not known as a command \nEnter 'info' to view all commands");
}
控制流量

当您到达第二个
ReadLine()
时,您可以键入任何内容,但当您这样做并键入enter时,程序将无事可做,因此退出


如果你想要一个永无止境的输入,你应该试着做一个循环。

只有一个问题-为什么它不应该关闭?你必须有一个while循环来检查用户输入或某些特定字符串,否则你的程序只会结束一个问题-为什么它不应该关闭?你必须有一个while循环来检查用户输入,或者某个特定字符串,否则您的程序结束只有一个问题-为什么它不应该关闭?您必须有一个while循环来检查用户输入,或者某个特定字符串,否则您的程序结束只有一个问题-为什么它不应该关闭?您必须有一个while循环来检查用户输入,或者某个特定字符串,否则你的程序就结束了谢谢你!这解决了我的问题!!我会在可能的时候标记为正确。我得等几分钟,谢谢!这解决了我的问题!!我会在可能的时候标记为正确。我得等几分钟,谢谢!这解决了我的问题!!我会在可能的时候标记为正确。我得等几分钟,谢谢!这解决了我的问题!!我会在可能的时候标记为正确。我得等几分钟。