C#简单的“Hello World”在控制台中缺少一行代码?

时间:2018-05-03 02:45:13

标签: c# visual-studio

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        string userName = "";
        int userAge = 0;
        int currentYear = 0;

        Console.Write("Please enter your name: ");
            userName = Console.ReadLine();
        Console.Write("Please enter your age: ");
        userAge = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please enter the current year: ");
        currentYear = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Hello World, my name is {0} and I'm {1} years old, I was born on the year {2} .", userName, userAge, currentYear - userAge);
    }
}
}

由于某种原因缺少最后一个Console.WriteLine,在我输入currentYear之后,它关闭应用程序而不将字符串显示到控制台。我刚刚开始学习c#所以也欢迎任何其他来源帮助我学习,谢谢! (顺便说一句,我使用视觉工作室)

1 个答案:

答案 0 :(得分:7)

您需要在代码底部添加Console.Read或Console.ReadLine

Console.ReadLine();

否则控制台将关闭,因为代码块已经执行。

附注:此问题可能重复 Why is the console window closing immediately without displaying my output?