zoukankan      html  css  js  c++  java
  • 简单数据类型转换

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

    namespace ConsoleProgamming
    {
        class Program
        {
            static void Main(string[] args)
            {
                //简单数据类型转换
                //为什么要使用数据类型转换?因为C#中Console.ReadLine()输入的都是字符串,
                //使用前要转换成你所期望的类型
                /*有两种方法完成转换:
                 1、目标数据类型.Parse(初始类型);
                 2、Convert.To目标数据类型(初始类型);*/
                //下面分别把输入的字符串转换成double,int和char类型(string在这里是陪衬,不需要给她转换类型)
                double d, d1;
                int i;
                string str;
                char c;
                Console.WriteLine("请输入一个小数:");
                d = Double.Parse(Console.ReadLine());//使用Double类的Parse静态函数把sring类型的数据转换成double类型
                Console.WriteLine("请输入一个小数:");
                d1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入一个整数:");
                i = Int32.Parse(Console.ReadLine());//使用Int32类的Parse静态函数把sring类型的数据转换成double类型
                Console.WriteLine("请输入一个字符:");
                c = Char.Parse(Console.ReadLine());//使用Char类的Parse静态函数把sring类型的数据转换成double类型
                Console.WriteLine("请输入一行字符串:");
                str = Console.ReadLine();

                Console.WriteLine("d={0},i={1},c={2},str={3},d1={4}", d, i, c, str, d1);

                //转义符只针对在代码中直接写出的字符串,对于程序中直接读出来字符串没有这个问题
                /* 输出:
         请输入一个小数:
         2.23
         请输入一个小数:
         3.4
         请输入一个整数:
         3
         请输入一个字符:
         f
         请输入一行字符串:
         fffff
         d=2.23,i=3,c=f,str=fffff,d1=3.4*/
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    November 07th, 2017 Week 45th Tuesday
    November 06th, 2017 Week 45th Monday
    November 05th, 2017 Week 45th Sunday
    November 04th, 2017 Week 44th Saturday
    November 03rd, 2017 Week 44th Friday
    Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )
    图片方向 image orientation Exif
    Asp.net core 学习笔记 ( Router 路由 )
    Asp.net core 学习笔记 ( Configuration 配置 )
    qrcode render 二维码扫描读取
  • 原文地址:https://www.cnblogs.com/lcyuhe/p/3747840.html
Copyright © 2011-2022 走看看