使用控制台编写程序 Hello World

使用 vs 创建控制台项目

例1.namespace Hello_World //程序的命名空间
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");//控制台输出语句
            Console.ReadLine();
        }
    }
}

例2.获取从控制台输入的值并输出

namespace Hello_World //程序的命名空间
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你的名字");
            string name = Console.ReadLine();
            Console.WriteLine("欢迎:"+name);
            Console.ReadLine();
        }
    }
}
例3.数据类型的转换

常见数据类型:int(整型 整数) double(双精度浮点型 小数) string(字符串) decimal(定点数) datetime (日期类型)

 //转化为string类型
            string name = Console.ReadLine();
            //转化为int类型
            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(age);
            //转化为double类型
            double weight = Convert.ToDouble(Console.ReadLine());
            //转化为decimal类型
            decimal salary = Convert.ToDecimal(Console.ReadLine());
            //转化为bool类型
            bool sex = Convert.ToBoolean(Console.ReadLine());
    //转化为datetime类型
            DateTime borndate = Convert.ToDateTime(Console.ReadLine());

 

猜你喜欢

转载自www.cnblogs.com/damoguxing/p/10779279.html