使用Console.ReadLine()无法显示来自用户的输入

时间:2019-02-08 12:36:06

标签: c#

我是C#和.NET代码/ VSCcode的新手。
当前使用VSCode运行和构建C#代码。

在声明要保留的特定变量之后,我无法使用Console.ReadLine()接受用户输入。终端在程序结束时生成此错误代码:

  

程序“ [4544] beyond.dll”已退出,代码为0(0x0)。

using System;

namespace forth
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("This is the first program we are trying to build");
            var  name = Console.ReadLine();

         Console.WriteLine("Hello " + name, "you successfully entered a name");


         Console.WriteLine("Hello " + name, "you successfully entered a name");

         Console.WriteLine("you can press enter to exit the program");

         Console.ReadLine();


        }
    }
}

输出“请输入您的姓名”并显示“你好+用户名”
该程序应随后存在

2 个答案:

答案 0 :(得分:2)

尝试将名称后的逗号移动到字符串内部,然后将第二个字符串附加到第一个字符串:

Console.WriteLine("Hello " + name + ", you successfully entered a name");

当我认为您只需要WriteLine(string)时,您将使用WriteLine(string,string)重载

答案 1 :(得分:1)

在创建项目时,您选择了错误的类型。您需要选择“新控制台应用程序”。

现在,您正在构建库,并且库没有Main方法。好吧,它可以有,但是没有被调用。因此,创建一个新项目,选择正确的类型并复制代码。

相关问题