变量的更多内容

今天坚持学习了“变量的更多内容”,感觉有些生涩,但还是坚持了下来,我相信“持之以恒”是最关键的!

 

1、类型转换

(1)隐式转换:自动转换,只要在范围内。

(2)显示转换:需要编写相应的代码。


2、显示转换

    明确要求编译器把数值从一种数据类型转换为另一种数据类型。

    (目标类型)初始类型

    转换时可以使用Convert命令进行显示转换,

eg:static void Main(string[] args)
        {
            short shortResult, shortVal = 4;                    //short 在-32768~32768之间的整数
            int integerVal = 67;                                       //int 在-2147483648~2147483648之间的整数
            long longResult;                    //long 在-9223372036854775808~9223372036854775808之间的整数
            float floatVal = 10.5f;            //float 有小数点的1.5*10的负45次方~3.4*10的正38次方
            double doubleResult, doubleVal = 99.999;   //double 有小数点5.0*10负324次方~1.7*10的正308次方
            string stringResult, stringVal = "17";            //string 一组字符
            bool boolVal = true;                                     //bool 逻辑值:true或false

 

            Console.WriteLine("Variable Conversion Examples/n");

 

            doubleResult = floatVal * shortVal;
            Console.WriteLine("Implicit,->double:{0}*{1}->{2}",floatVal,shortVal,doubleResult);

 

            shortResult = (short)floatVal;
            Console.WriteLine("Explicit,->short:{0}->{1}", floatVal, shortResult);

 

            stringResult = Convert.ToString(boolVal) + Convert.ToString(doubleVal);
            Console.WriteLine("Explicit,->string:/"{0}/"*/"{1}/"->{2}", boolVal, doubleVal, stringResult);

 

            longResult = integerVal + Convert.ToInt64(stringVal);
            Console.WriteLine("Mixed,->long:{0}+{1}->{2}",integerVal,stringVal,longResult);

            Console.ReadKey();
        }

 

示例说明:各种类型的各宗转换。


3、枚举

     枚举允许定义一个类型,其中包含提供的限定值集合中的一个值。

     枚举使用“enum”关键字来定义。

     基本类型可以是:byte/sbyte/short/ushort/int/uint/long/ulong.

eg:

namespace Ch05Ex02
{
    enum orientation : byte //orientation表示方向,byte在0~255之间的整数
    {
        north=1,
        south=2,
        east=3,
        west=4
    }

    class Program
    {
        static void Main(string[] args)
        {
            byte directionByte;
            string directionString;
            orientation myDirection = orientation.north;
            Console.WriteLine("myDirection={0}",myDirection);
            directionByte = (byte)myDirection;
            directionString = Convert.ToString(myDirection);
            Console.WriteLine("byte equivalent={0}",directionByte);
            Console.WriteLine("string equivalent={0}",directionString);
            Console.ReadKey();
        }
    }
}

 

示例说明:枚举示例


4、结构

     结构是由几个数据组成的数据结构,这些数据可有不同的类型,根据这个结构,定义自己的变量类型。

     结构使用“struct”关键字来定义。

     要让调用结构的代码访问该结构的数据成员,使用关键字“public”。

eg:

namespace Ch05Ex03
{
    enum orientation : byte //枚举:北、南、东、西四个方向
    {
        north=1,
        south=2,
        east=3,
        weat=4,
    }
    struct route //结构:路由(一个方向和一个距离值组成)
    {
        public orientation direction; //方向
        public double distance; //距离
    }

    class Program
    {
        static void Main(string[] args)
        {
            route myRoute;
            int myDirection = -1;
            double myDistance;
            Console.WriteLine("1) North/n2) South/n3) East/n4) West");
            do
            {
                Console.WriteLine("Select a direction:"); //选择一个方向
                myDirection = Convert.ToInt32(Console.ReadLine());//转化为Int型
            }
            while ((myDirection < 1) || (myDirection > 4));
            Console.WriteLine("Input a distance:");
            myDistance = Convert.ToDouble(Console.ReadLine());//转化为Double型
            myRoute.direction = (orientation)myDirection;
            myRoute.distance = myDistance;
            Console.WriteLine("myRoute specifies a direction of {0} and a" + "distance of {1}", myRoute.direction, myRoute.distance);
            Console.ReadKey();
        }
    }
}

 

示例说明:结构和枚举同在一个示例。


5、数组

     数组是一个变量的下标列表,存储在数组类型的变量中;数组有一个基本类型,数组中的各个条目都是这种类型;数组的条目常称为“元素”。

     数组必须在访问之前初始化:

(1)可以以字面形式指定数组的完整内容;

eg:int[] myIntArray={5,9,10,2,99};

(2)可以指定数组的大小,再使用关键字“new”初始化所有的数组元素

eg:int[] myIntArray;

      myIntArray=new int[5];

 

eg:static void Main(string[] args)
        {
            string[] friendNames = { "Kitchanin", "Naree", "Khaopan" };//数组的初始化第一种方式
            int i;
            Console.WriteLine("Here are {0} of my friends:",friendNames.Length); //使用“friendNames.Length”来确定数组中元素的个数
            for (i = 0; i < friendNames.Length; i++)
            {
                Console.WriteLine(friendNames[i]);
            }
            Console.ReadKey();
        }

 

示例说明:数组示例


6、foreach循环

     foreach循环回迭代每个元素,依次把每个元素放在变量<name>中,且不存在访问非法元素的危险。不需要考虑数组中有多少个元素,并可以确保将在循环中使用每个元素。foreach循环对数组内容进行只读访问,所以不能改变任何元素的值。

 

eg:static void Main(string[] args)
        {
            string[] friendNames = { "Kitchanin", "Naree", "Khaopan" };//数组的初始化第一种方式
            Console.WriteLine("Here are {0} of my friends:", friendNames.Length); //使用“friendNames.Length”来确定数组中元素的个数
            foreach (string friendName in friendNames)
            {
                Console.WriteLine(friendName);
            }
            Console.ReadKey();
        }

 

示例说明:foreach循环示例


7、“多维数组”和“数组的数组”

“多维数组”是使用多个下标访问其元素的数组。另称:矩形数组

eg: double[,] hillHeight = new double[3,4];

 

“数组的数组”

声明“数组的数组”其语法要在数组的声明中指定多个方括号对,两种方式:

 

(1)可以初始化包含其他数组的数组(为了清晰起见,称之为子数组),然后依次初始化子数组:

eg:jaggedIntArray= new int [2] [];

       jaggedIntArray [0]= new int[3];

       jaggedIntArray [1]= new int[4];

 

(2)也可以使用字面值赋值的一种改进形式:

eg:jaggedIntArray = new int[3] [] {new int[] {1,2,3}, new int[] {1}, new int[] {1,2}};


8、字符串的处理

为了获得一个可写的char数组,可以使用数组变的<string>ToCharArray()方法。

 

有效的命令:

(1)<string>.ToLower() 和 <string>.ToUpper()  // 可以把字符串转换为大写或小写形式

(2)<string>.Trim()  // 删除输入字符串中的空格

(3)<string>.TrimStart() 和 <string>.TrimEnd()  // 把字符串前面或后面的空格删除

(4)<string>.PadLegt()  和 <string>.PadRight() // 可以在字符串的左边或后边添加空格,使字符串达到指定长度

 

eg:static void Main(string[] args)
        {
            string myString = "This is a test.";
            char[] separator = {' '};//separator:分隔器
            string[] myWords;
            myWords=myString.Split(separator); //<string>.Split()把string转换为string数组,把它在指定的位置分隔开{' '},这些位置采用char数组的形式
            foreach (string word in myWords)
            {
                Console.WriteLine("{0}",word);
            }
            Console.ReadKey();
        }

 

示例说明:使用命令<string>.Split把string转换为string数组,把它在指定的位置隔开{' '},这些位置采用char数组的形式


9、三个实用示例

(1)用户输入一个字符串,将其中的字符与输入相反的顺序输出:

 

eg:static void Main(string[] args) //用户输入一个字符串,将其中的字符以输入相反的顺序输出
        {
            Console.WriteLine("Enter a string:");
            string myString = Console.ReadLine();
            string reversedString = "";
            for (int index = myString.Length - 1; index >= 0; index--)
            {
                reversedString += myString[index];
            }
            Console.WriteLine("Reversed: {0}", reversedString);
            Console.ReadKey();
        }

 

(2)用户输入一个字符串,用“yes”替换字符串中的所有的“no”:

 

eg:static void Main(string[] args) //接收一个字符串,用yes替换字符串中所有的no
        {
            Console.WriteLine("Enter a string:");
            string myString = Console.ReadLine();
            myString = myString.Replace("no", "yes"); //Replace:“替换”命令,使用“查找和替换”窗口的“快速替换”选项卡中的可用选项子集替换文件中的文本。
            Console.WriteLine("Replaced /"no/" with /"yes/": {0}",myString);
            Console.ReadKey();
        }

 

(3)给用户输的字符串中的每个单词加上双引号:

 

eg:方法一:

static void Main(string[] args) //给字符串中的每个单词都加上双引号
        {
            Console.WriteLine("Enter a string:");
            string myString = Console.ReadLine();
            myString = "/"" + myString.Replace(" ", "/" /"") + "/"";
            Console.WriteLine("Added double quotes areound words: {0}",myString);
            Console.ReadKey();
        }

 

方法二:

static void Main(string[] args) //给字符串中的每个单词都加上双引号
        {
            Console.WriteLine("Enter a string:");
            string myString = Console.ReadLine();
            string[] myWords = myString.Split(' '); //Split:返回的字符串数组包含此实例中的子字符串(由指定字符串或 Unicode 字符数组的元素分隔)。
            Console.WriteLine("Adding double quotes areound words:");
            foreach (string myWord in myWords)
            {
                Console.Write("/"{0}/" ", myWord);
            }
            Console.ReadKey();
        }


哈哈!QQ糖豆今天又学了一些新的知识,虽然感觉挺生涩的,但是感觉“越嚼越有味”,学程序“持之以恒”最重要,加油吧!听说明天的“函数”挺难,挺重要的,但是QQ糖豆接受挑战,被人牵着鼻子走的感觉真难受!为了预订的目标加油!!!!!

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值