0505.Net基础班第六天(复杂数据类型)

1、变量类型 int double string char bool decimal 变量的使用规则:先声明再赋值最后使用 int number; number=10;  number=20; Console.WriteLine(number);

2、Camel  Pascal

3、运算符 赋值运算符:= 复合赋值运算符:+= -= *= /= %=  sum+=age;  sum=sum+age; 算数运算符: + - * / % ++ -- 关系运算符: > < >= <= == != 逻辑运算符: && || !

4、c#中的语法结构 分支结构: if if-else 选择结构: while do-while for

5、常量 声明的常量的语法: const 变量类型 变量名=值; 什么时候会用到常量?

6、枚举 语法: [public] enum 枚举名 {  值1,  值2,  值3,  ........ } public:访问修饰符。公开的公共的,哪都可以访问。 enum:关键字,声明枚举的关键字 枚举名:要符合Pascal命名规范

将枚举声明到命名空间的下面,类的外面,表示这个命名空间下,所有的类都可以使用这个枚举。

枚举就是一个变量类型 ,int--double  string  decimal. 只是枚举声明、赋值、使用的方式跟那些普通的变量类型不一样。

我们可以将一个枚举类型的变量跟int类型和string类型互相转换。 枚举类型默认是跟int类型相互兼容的,所以可以通过强制类型转换的语法互相转换。 当转换一个枚举中没有的值的时候,不会抛异常,而是直接将数字显示出来。

枚举同样也可以跟string类型互相转换,如果将枚举类型转换成string类型,则直接调用ToString(). 如果将字符串转换成枚举类型则需要下面这样一行代码:  (要转换的枚举类型)Enum.Parse(typeof(要转换的枚举类型),"要转换的字符串"); 如果转换的字符串是数字,则就算枚举中没有,也会不会抛异常。 如果转换的字符串是文本,如果枚举中没有,则会抛出异常。

7、所有的类型都能够转换成string类型,调用ToString()。

8、结构 可以帮助我们一次性声明多个不同类型的变量。 语法: [public] struct 结构名 {  成员;//字段 } 变量在程序运行期间只能存储一个值,而字段可以存储多个值。

9、数组 一次性存储多个相同类型的变量。 语法: 数组类型[] 数组名=new 数组类型[数组长度];

***数组的长度一旦固定了,就不能再被改变了

10、冒泡排序:就是将一个数组中的元素按照从大到小或者从小到大的顺序进行排列。 int[] nums={9,8,7,6,5,4,3,2,1,0}; 0 1 2 3 4 5 6 7 8 9 第一趟比较:8 7 6 5 4 3 2 1 0 9 交换了9次     i=0  j=nums.Length-1-i 第二趟比较:7 6 5 4 3 2 1 0 8 9 交换了8次     i=1  j=nums.Length-1-i 第三趟比较:6 5 4 3 2 1 0 7 8 9 交换了7次     i=2  j=nums.Length-1-i 第四趟比较:5 4 3 2 1 0 6 7 8 9 交换了6次     i=3  j=nums.Length-1-i 第五趟比较:4 3 2 1 0 5 6 7 8 9 交换了5次 第六趟比较:3 2 1 0 4 5 6 7 8 9 交换了4次 第七趟比较:2 1 0 3 4 5 6 7 8 9 交换了3次 第八趟比较:1 0 2 3 4 5 6 7 8 9 交换了2次 第九趟比较:0 1 2 3 4 5 6 7 8 9 交换了1次 for(int i=0;i<number.Length-1;i++) {  for(int j=0;j<nums.Length-1-i;j++)  {   if(nums[j]>nums[j+1])   {    int temp=nums[j];    nums[j]=nums[j+1];    nums[j+1]=temp;   }  } }

11、方法 函数就是将一堆代码进行重用的一种机制。 函数的语法: [public] static 返回值类型 方法名([参数列表]) {  方法体; } public:访问修饰符,公开的,公共的,哪都可以访问。 static:静态的 返回值类型:如果不需要写返回值,写void 方法名:Pascal 每个单词的首字母都大些。其余字母小写 参数列表:完成这个方法所必须要提供给这个方法的条件。如果没有参数,小括号也不能省略。

方法写好后,如果想要被执行,必须要在Main()函数中调用。 方法的调用语法: 类名.方法名([参数]); ***在某些情况下,类名是可以省略的,如果你写的方法跟Main()函数同在一个类中,这个时候, 类名可以省略。

12、return 1、在方法中返回要返回的值。 2、立即结束本次方法。

01复习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _01复习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int number = 10;
14             number = 20;//体现了变量可以被重新赋值
15 
16             const int numberTwo = 50;//常量 不能够被重新赋值
17             //numberTwo = 90;
18         }
19     }
20 }
View Code

02枚举

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _02枚举
 8 {
 9     //
10         //    [public] enum 枚举名
11         //{
12         //    值1,
13         //    值2,
14         //    值3,
15         //    ........
16         //}
17 
18 
19     //声明了一个枚举 Gender
20     public enum Gender
21     {
22         男,
23 24     }
25 
26     class Program
27     {
28 
29         static void Main(string[] args)
30         {
31             //变量类型 变量名=值;
32             int n = 10;
33             Gender gender = Gender.男;
34 
35             //为什么会有枚举这个东东?
36             //xx大学管理系统
37             //姓名 性别 年龄 系别 年级  
38             //性别
39             //char gender = '男';
40             //string s1 = "female";
41             //string ss1 = "爷们";
42         }
43     }
44 }
View Code

03枚举的练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _03枚举的练习
 8 {
 9     public enum Sesons
10     { 
11         春,
12         夏,
13         秋,
14 15     }
16 
17     public enum QQState
18     { 
19         OnLine,
20         OffLine,
21         Leave,
22         Busy,
23         QMe
24     }
25 
26 
27     class Program
28     {
29         static void Main(string[] args)
30         {
31             Sesons s = Sesons.春;
32             QQState state = QQState.OnLine;
33 
34         }
35     }
36 }
View Code

04枚举和int以及string类型之间的转换

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _04枚举和int以及string类型之间的转换
  8 {
  9     public enum QQState
 10     {
 11         OnLine=1,
 12         OffLine,
 13         Leave,
 14         Busy,
 15         QMe
 16     }
 17 
 18     public enum Gender
 19     {
 20         男,
 21  22     }
 23     class Program
 24     {
 25         static void Main(string[] args)
 26         {
 27             #region 将枚举类型强转成int类型
 28             //QQState state = QQState.OnLine;
 29             ////枚举类型默认可以跟int类型互相转换  枚举类型跟int类型是兼容的
 30             //int n = (int)state;
 31             //Console.WriteLine(n);
 32             //Console.WriteLine((int)QQState.OffLine);
 33             //Console.WriteLine((int)QQState.Leave);
 34             //Console.WriteLine((int)QQState.Busy);
 35             //Console.WriteLine((int)QQState.QMe);
 36             //Console.ReadKey();
 37             #endregion
 38             #region 将int类型强转为枚举类型
 39 
 40             //int n1 = 3;
 41 
 42             //QQState state = (QQState)n1;
 43             //Console.WriteLine(state);
 44             //Console.ReadKey();
 45             #endregion
 46             #region 将枚举类型转换成字符串类型
 47             //所有的类型都能够转换成string类型
 48             // int n1 = 10;
 49             //// double n1 = 3.14;
 50             // decimal n1 = 5000m;
 51             // string s = n1.ToString();
 52             // Console.WriteLine(s);
 53             // Console.ReadKey();
 54 
 55             //QQState state = QQState.OnLine;
 56             //string s = state.ToString();
 57             //Console.WriteLine(s);
 58             //Console.ReadKey();
 59             #endregion
 60             #region 将字符串类型转换为枚举类型
 61             //string s = "ABCDEFG";
 62             ////将s转换成枚举类型
 63             ////Convert.ToInt32()  int.parse()  int.TryParse()
 64             ////调用Parse()方法的目的就是为了让它帮助我们将一个字符串转换成对应的枚举类型
 65             ////
 66             //QQState state = (QQState)Enum.Parse(typeof(QQState), s);
 67             //Console.WriteLine(state);
 68             //Console.ReadKey();
 69             #endregion
 70 
 71 
 72 
 73             //枚举练习
 74             //提示用户选择一个在线状态,我们接受,并将用户的输入转换成枚举类型。
 75             //再次打印到控制台中
 76 
 77             Console.WriteLine("请选择您的qq在线状态 1--OnLine 2--OffLine 3--Leave 4--Busy 5--QMe");
 78             string input = Console.ReadLine();
 79             switch (input)
 80             {
 81                 case "1": QQState s1 = (QQState)Enum.Parse(typeof(QQState), input);
 82                     Console.WriteLine("您选择的在线状态是{0}",s1);
 83                     break;
 84                 case "2": QQState s2 = (QQState)Enum.Parse(typeof(QQState), input);
 85                      Console.WriteLine("您选择的在线状态是{0}",s2);
 86                     break;
 87                 case "3": QQState s3 = (QQState)Enum.Parse(typeof(QQState), input);
 88                      Console.WriteLine("您选择的在线状态是{0}",s3);
 89                     break;
 90                 case "4": QQState s4 = (QQState)Enum.Parse(typeof(QQState), input);
 91                     Console.WriteLine("您选择的在线状态是{0}", s4);
 92                     break;
 93                 case "5": QQState s5 = (QQState)Enum.Parse(typeof(QQState), input);
 94                     Console.WriteLine("您选择的在线状态是{0}", s5);
 95                     break;
 96             }
 97             Console.ReadKey();
 98         }
 99     }
100 }
View Code

05结构

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _05结构
 8 {
 9     public struct Person
10     {
11         public string _name;//字段
12         public int _age;
13         public Gender _gender;
14     }
15 
16     public enum Gender
17     { 
18         男,
19 20     }
21 
22     class Program
23     {
24         static void Main(string[] args)
25         {
26             //XX大学管理系统
27             //姓名、性别、年龄、年级  //5000  20000
28             //string zsName = "张三";
29             //int zsAge = 21;
30             //char zsGender = '男';
31             //int zsGrade = 3;
32 
33              string s = "123";
34 
35             Person zsPerson;
36             zsPerson._name = "张三";
37             zsPerson._age = 21;
38             zsPerson._gender = Gender.男;
39 
40 
41             Person lsPerson;
42             lsPerson._name = "李四";
43             lsPerson._age = 22;
44             lsPerson._gender = Gender.女;
45 
46 
47             Console.WriteLine(zsPerson._name);
48             Console.WriteLine(lsPerson._name);
49             Console.ReadKey();
50 
51         }
52     }
53 }
View Code

06结构练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _06结构练习
 8 {
 9 
10     public struct MyColor
11     {
12         public int _red;
13         public int _green;
14         public int _blue;
15     }
16 
17 
18     public enum Gender
19     { 
20         男,
21 22     }
23 
24 
25     public struct Person
26     {
27         public string _name;
28         public int _age;
29         public Gender _gender;
30     }
31 
32 
33 
34 
35     class Program
36     {
37         static void Main(string[] args)
38         {
39             //1 定义一个结构叫MyColor,有三个成员,分别定义为int类型的red,green,blue
40              //声明一个 MyColor类型的变量,并对其成员赋值.使MyColor可以表示成一个红色.
41             //MyColor mc;
42             //mc._red = 255;
43             //mc._blue = 0;
44             //mc._green = 0;
45 
46 
47             //2 定义一个结构类型Person,有三个成员,分别为姓名,性别,年龄 性别用枚举类型
48             //声明两个Person类型的变量,分别表示 张三 男  18岁/ 小兰 女 16岁
49 
50 
51             Person zsPerson;
52             zsPerson._name = "张三";
53             zsPerson._gender = Gender.男;
54             zsPerson._age = 18;
55 
56 
57             Person xlPerson;
58             xlPerson._name = "小兰";
59             xlPerson._gender = Gender.女;
60             xlPerson._age = 16;
61 
62 
63             Console.WriteLine("我叫{1},我今年{0}岁了,我是{2}生", zsPerson._name, zsPerson._age, zsPerson._gender);
64             Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生",xlPerson._name,xlPerson._age,xlPerson._gender);
65             Console.ReadKey();
66 
67         }
68     }
69 }
View Code

07数组

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _07数组
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //数组类型[] 数组名=new 数组类型[数组长度];
14             int[] nums = new int[10];
15             //数组的声明方式
16             int[] numsTwo = { 1, 2, 3, 4, 5, 6 };
17             //string[] str = new string[10];
18             ////null ""
19             //bool[] bools = new bool[10];
20             //Console.ReadKey();
21             //nums[0] = 1;
22             //nums[1] = 2;
23             //nums[2] = 3;
24             //nums[3] = 4;
25             //nums[4] = 5;
26             //nums[5] = 6;
27             //nums[6] = 7;
28             //nums[7] = 8;
29             //nums[8] = 9;
30             //nums[9] = 10;
31             //nums[10] = 11;
32             ////我们通过一个循环给数组赋值,同样,也通过一个循环对数组进行取值
33             ////for (int i = 0; i < nums.Length; i++)
34             ////{
35             ////    nums[i] = i;
36             ////}
37             //for (int i = 0; i < nums.Length; i++)
38             //{
39             //    Console.WriteLine(nums[i]);
40             //}
41             Console.ReadKey();
42         }
43     }
44 }
View Code

08数组的5个练习

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _08数组的5个练习
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             #region 练习1
 14             ////练习1:从一个整数数组中取出最大的整数,最小整数,总和,平均值
 15             ////声明一个int类型的数组 并且随意的赋初值
 16             //int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 17             ////声明两个变量用来存储最大值和最小值
 18             //int max = int.MinValue;//nums[3];
 19             //int min = int.MaxValue;//nums[0];
 20             //int sum = 0;
 21             ////循环的让数组中的每个元素跟我的最大值、最小值进行比较
 22 
 23             //for (int i = 0; i < nums.Length; i++)
 24             //{
 25             //    //关于在循环中nums[i]的理解方式
 26             //    //1、代表数组中当前循环到的元素
 27             //    //2、代表数组中的每个元素
 28             //    //如果数组中当前循环到的这个元素 比我的max还要大,则把当前这个元素赋值给我的max
 29             //    if (nums[i] > max)
 30             //    {
 31             //        max = nums[i];
 32             //    }
 33 
 34             //    if (nums[i] < min)
 35             //    {
 36             //        min = nums[i];
 37             //    }
 38             //    sum += nums[i];
 39             //}
 40             //Console.WriteLine("这个数组的最大值是{0},最小值是{1},总和是{2},平均值是{3}", max, min, sum, sum / nums.Length);
 41             //Console.ReadKey(); 
 42             #endregion
 43 
 44 
 45 
 46             //练习3:数组里面都是人的名字,分割成:例如:老杨|老苏|老邹…”
 47             //(老杨,老苏,老邹,老虎,老牛,老蒋,老王,老马)
 48 
 49             //string[] names = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" };
 50             ////老杨|老苏|老邹|老虎|老牛|老马
 51             ////解体思路:通过一个循环,获得字符串数组中的每一个元素。
 52             ////然后,将这个每一个元素都累加到一个字符串中,以|分割
 53             //string str=null;//""
 54             //for (int i = 0; i < names.Length-1; i++)
 55             //{
 56             //    str += names[i]+"|";
 57             //}
 58             //Console.WriteLine(str+names[names.Length-1]);
 59             //Console.ReadKey();
 60 
 61 
 62             #region 练习4
 63             //练习4:将一个整数数组的每一个元素进行如下的处理:
 64             //如果元素是正数则将这个位置的元素的值加1,
 65             //如果元素是负数则将这个位置的元素的值减1,如果元素是0,则不变。
 66 
 67             //int[] nums = { 1, -2, 3, -4, 5, 6, 0 };
 68             ////解题思路:通过一个循环,获得数组中的每一个元素。
 69             ////对每一个元素进行判断
 70             //for (int i = 0; i < nums.Length; i++)
 71             //{
 72             //    if (nums[i] > 0)
 73             //    {
 74             //        nums[i] += 1;
 75             //    }
 76             //    else if (nums[i] < 0)
 77             //    {
 78             //        nums[i] -= 1;
 79             //    }
 80             //    else
 81             //    {
 82             //        //nums[i] = 0;
 83             //    }
 84             //}
 85 
 86             //for (int i = 0; i < nums.Length; i++)
 87             //{
 88             //    Console.WriteLine(nums[i]);
 89             //}
 90             //Console.ReadKey();
 91 
 92             #endregion
 93 
 94 
 95 
 96             //练习5:将一个字符串数组的元素的顺序进行反转。{“我”,“是”,”好人”} {“好人”,”是”,”我”}。第i个和第length-i-1个进行交换。
 97             string[] names = { "a", "b", "c", "d", "e", "f", "g" };
 98             for (int i = 0; i < names.Length / 2; i++)
 99             {
100                 string temp = names[i];
101                 names[i] = names[names.Length - 1 - i];
102                 names[names.Length - 1 - i] = temp;
103             }
104 
105             for (int i = 0; i < names.Length; i++)
106             {
107                 Console.WriteLine(names[i]);
108             }
109             Console.ReadKey();
110 
111 
112             //{"好人","是","我"};
113         }
114     }
115 }
View Code

09冒泡排序

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _09冒泡排序
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] nums = { 1, 4, 3, 9, 6, 8, 11 };
14             //只能针对数组做一个升序的排列
15             //Array.Sort(nums);
16 
17             //对数组进行反转
18             //Array.Reverse(nums);
19 
20             for (int i = 0; i < nums.Length-1; i++)
21             {
22                 for (int j = 0; j < nums.Length - 1 - i; j++)
23                 {
24                     if (nums[j] < nums[j + 1])
25                     {
26                         int temp = nums[j];
27                         nums[j] = nums[j + 1];
28                         nums[j + 1] = temp;
29                     }
30                 }
31             }
32 
33             for (int i = 0; i < nums.Length; i++)
34             {
35                 Console.WriteLine(nums[i]);
36             }
37             Console.ReadKey();
38 
39                 //for (int i = 0; i < nums.Length - 1; i++)
40                 //{
41                 //    for (int j = 0; j < nums.Length - 1-i ; j++)
42                 //    {
43                 //        if (nums[j] > nums[j + 1])
44                 //        {
45                 //            int temp = nums[j];
46                 //            nums[j] = nums[j + 1];
47                 //            nums[j + 1] = temp;
48                 //        }
49                 //    }
50                 //}
51 
52 
53                 for (int i = 0; i < nums.Length; i++)
54                 {
55                     Console.WriteLine(nums[i]);
56                 }
57             Console.ReadKey();
58 
59 
60 
61         }
62     }
63 }
View Code

10方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _10方法
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //闪烁  播放一段特殊的背景音乐  屏幕停止
14             Program.PlayGame();
15             Program.WuDi();
16             Program.PlayGame();
17             Program.PlayGame();
18             Program.PlayGame();
19             Program.PlayGame();
20             Program.WuDi();
21             Console.ReadKey();
22             
23            
24         }
25 
26         /// <summary>
27         /// 正常玩游戏
28         /// </summary>
29         public static void PlayGame()
30         {
31             Console.WriteLine("超级玛丽走呀走,跳呀跳,顶呀顶");
32             Console.WriteLine("超级玛丽走呀走,跳呀跳,顶呀顶");
33             Console.WriteLine("超级玛丽走呀走,跳呀跳,顶呀顶");
34             Console.WriteLine("超级玛丽走呀走,跳呀跳,顶呀顶");
35             Console.WriteLine("超级玛丽走呀走,跳呀跳,顶呀顶");
36             Console.WriteLine("突然,顶到了一个无敌");
37         }
38         /// <summary>
39         /// 无敌
40         /// </summary>
41         public static void WuDi()
42         {
43             Console.WriteLine("屏幕开始闪烁");
44             Console.WriteLine("播放无敌的背景音乐");
45             Console.WriteLine("屏幕停止");
46         }
47 
48     }
49 }
View Code

11、方法练习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _11_方法练习
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //计算两个整数之间的最大值
14             int max = Program.GetMax(1, 3);
15             Console.WriteLine(max);
16            // Array.Sort()
17             // int n = Convert.ToInt32("123");
18 
19             string str = Console.ReadLine();
20 
21             Console.ReadKey();
22         }
23 
24 
25         /// <summary>
26         /// 计算两个整数之间的最大值并且将最大值返回
27         /// </summary>
28         /// <param name="n1">第一个整数</param>
29         /// <param name="n2">第二个整数</param>
30         /// <returns>将最大值返回</returns>
31         public static int GetMax(int n1, int n2)
32         {
33             return n1 > n2 ? n1 : n2;
34         }
35 
36     }
37 }
View Code

12、return

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _12_return
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13 
14             while (true)
15             {
16                 Console.WriteLine("Hello World");
17                 //break;
18                // continue;
19                 return;
20             }
21             Console.WriteLine("Hello .Net");
22             Console.ReadKey();
23         }
24     }
25 }
View Code
原文地址:https://www.cnblogs.com/liuslayer/p/4713351.html