C#和.Ne学习第六天

摘要: (主要是传智播客的赵剑宇老师上课的笔记自己在加上一点自己的理解,在此感谢赵剑宇老师)

1、变量类型
int double string char bool decimal
变量的使用规则:先声明再赋值最后使用

1 int number;
2 number=10;
3 number=20;
4 Console.WriteLine(number);

2、Camel Pascal

Camel:

主要用于变量的命名

骆驼式命名法就是当变量名或函式名是由一个或多个单词连结在一起,而构成的唯一识别字时,第一个单词以小写字母开始;第二个单词的首字母大写或每一个单词的首字母都采用大写字母,例如:myFirstName、myLastName,这样的变量名看上去就像骆驼峰一样此起彼伏,故得名。

Pascal:

主要用于函数和方法类的命名

单字之间不以空格断开或连接号(-)、底线(_)连结,第一个单字首字母采用大写字母;后续单字的首字母亦用大写字母,例如:FirstName、LastName。每一个单字的首字母都采用大写字母的命名格式,被称为“Pascal命名法”,源自于Pascal语言的命名惯例,也有人称之为“大驼峰式命名法”(Upper Camel Case),为驼峰式大小写的子集。

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(要转换的枚举类型),"要转换的字符串");
如果转换的字符串是数字,则就算枚举中没有,也会不会抛异常。
如果转换的字符串是文本,如果枚举中没有,则会抛出异常。
实例:
public enum Gender
{
男,

}
使用:
Gender gender = Gender.男;
注意:最后一个枚举中的元素可以加,也可以不加

枚举类型转换

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


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

字段前面加_线

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication4
 8 {
 9     class Program
10     {
11         public struct Color
12         {
13             public int _red;
14             public int _green;
15             public int _blue;
16         }
17         static void Main(string[] args)
18         {
19             Color color;
20             color._red = 255;
21             color._green = 0;
22             color._blue = 0;
23 
24             Console.WriteLine("红:{0}绿:{1}蓝:{2}", color._red, color._green, color._blue);
25             Console.ReadKey();
26         }
27     }
28 }
29 
30 输出结果:
31 红:255绿:0蓝:0
1
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication5
 8 {
 9     struct Person
10     {
11         public string name;
12         public Gender gender;
13         public int age;
14     }
15     public enum Gender
16     {
17         男,
18 19     }
20     class Program
21     {
22         static void Main(string[] args)
23         {
24             Person zsPerson;
25             zsPerson.name = "张三";
26             zsPerson.gender = Gender.男;
27             zsPerson.age = 19;
28 
29             Person xlPerson;
30             xlPerson.name = "小兰";
31             xlPerson.gender = Gender.女;
32             xlPerson.age = 16;
33 
34             Console.WriteLine("姓名:{0}性别:{1}年龄:{2}", zsPerson.name, zsPerson.gender, zsPerson.age);
35             Console.WriteLine("姓名:{0}性别:{1}年龄:{2}", xlPerson.name, xlPerson.gender, xlPerson.age);
36             Console.ReadKey();
37         }
38     }
39 }
40 
41 输出结果:
42 姓名:张三性别:男年龄:19
43 姓名:小兰性别:女年龄:16
2


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次

 1 for(int i=0;i<number.Length-1;i++)
 2 {
 3 for(int j=0;j<nums.Length-1-i;j++)
 4 {
 5 if(nums[j]>nums[j+1])
 6 {
 7 int temp=nums[j];
 8 nums[j]=nums[j+1];
 9 nums[j+1]=temp;
10 }
11 }
12 }

int数组默认0
bool数组默认flase
string默认是null
数组的几种声明方式:

1 1int []a = new int[10];
2 2int []a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
3 3int []a = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
4 4int []a = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 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 ConsoleApplication8
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int []a = {5, 21, 9, 2, 4, 8, 0, 11, 3, 6};
14 
15 
16             for (int t = 0; t < a.Length; ++t)
17             {
18                 Console.Write(a[t] + " ");
19             }
20             Console.WriteLine("\n");
21             Console.WriteLine("数组最大值:"+Program.ArrayMax(a));
22             Console.WriteLine("数组最小值:" + Program.ArrayMin(a));
23             Console.WriteLine("数组总和:" + Program.ArraySum(a));
24             Console.WriteLine("数组平均值:" + Program.ArrayAverage(a));
25             Console.ReadLine();
26         }
27 
28         /// <summary>
29         /// 计算整型数组的当中元素的最大值
30         /// </summary>
31         /// <param name="a">数组名</param>
32         /// <returns>返回数组中最大的元素</returns>
33         static int ArrayMax(int []a)
34         {
35             int Max = a[0];
36             for (int t = 1; t < a.Length; ++t)
37             {
38                 if (a[t] > Max)
39                 {
40                     Max = a[t];
41                 }
42             }
43             return Max;
44         }
45         /// <summary>
46         /// 计算整型数组的当中元素的最小值
47         /// </summary>
48         /// <param name="a">数组名</param>
49         /// <returns></returns>
50         static int ArrayMin(int []a)
51         {
52             int Min = a[0];
53             for (int t = 1; t < a.Length; ++t)
54             {
55                 if (a[t] < Min)
56                 {
57                     Min = a[t];
58                 }
59             }
60                 return Min;
61         }
62         /// <summary>
63         /// 计算整型数组的总和
64         /// </summary>
65         /// <param name="a">数组名</param>
66         /// <returns>返回数组的总和</returns>
67         static int ArraySum(int []a)
68         {
69             int sum = 0;
70 
71             for (int t = 0; t < a.Length; ++t)
72             {
73                 sum +=a[t];
74             }
75 
76                 return sum;
77         }
78         /// <summary>
79         /// 计算整型数组的平均值
80         /// </summary>
81         /// <param name="a">数组名</param>
82         /// <returns>返回这个数组的平均值</returns>
83         static int ArrayAverage(int[] a)
84         {
85             int Max = ArraySum(a);
86             return Max / a.Length;
87         }
88     }
89 }
90 
91 输出结果:
92 5 21 9 2 4 8 0 11 3 6
93 
94 数组最大值:21
95 数组最小值:0
96 数组总和:69
97 数组平均值:6
1
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication8
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] a = { 5, 21, 9, 2, 4, 8, 0, 11, 3, 6 };
14 
15 
16             for (int t = 0; t < a.Length; ++t)
17             {
18                 Console.Write(a[t] + " ");
19             }
20             Console.WriteLine("\n");
21             Console.WriteLine("数组总和:" + Program.ArraySum(a));
22             Console.ReadLine();
23         }
24         /// <summary>
25         /// 计算整型数组的总和
26         /// </summary>
27         /// <param name="a">数组名</param>
28         /// <returns>返回数组的总和</returns>
29         static int ArraySum(int[] a)
30         {
31             int sum = 0;
32 
33             for (int t = 0; t < a.Length; ++t)
34             {
35                 sum += a[t];
36             }
37 
38             return sum;
39         }
40     }
41 }
42 
43 输出结果:
44 5 21 9 2 4 8 0 11 3 6
45 
46 数组总和:69
2
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication10
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             String[] szName = new String[8];
14             szName[0] = "老杨";
15             szName[1] = "老苏";
16             szName[2] = "老邹";
17             szName[3] = "老虎";
18             szName[4] = "老牛";
19             szName[5] = "老蒋";
20             szName[6] = "老王";
21             szName[7] = "老马";
22 
23             for (int t = 0; t < szName.Length; ++t)
24             {
25                 Console.Write(szName[t]+" ");
26             }
27             Console.WriteLine("\n");
28             Console.ReadKey();
29         }
30     }
31 }
32 
33 输出结果:
34 老杨 老苏 老邹 老虎 老牛 老蒋 老王 老马
3
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication11
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] a = { 5, 21, 9, -2, 4, -8, 0, 11, -3, 6 };
14             Program.ArrayOut(a);
15             Console.Write("\n");
16             Program.Dispose(a);
17             Program.ArrayOut(a);
18             Console.Write("\n");
19             Console.ReadKey();
20         }
21         /*
22          * 将一个整数数组的每一个元素进行如下的处理
23          * 如果元素是正数則将这个位置的元素的值加1,
24          * 如果元素是负数则将这个位置的元素值减1,
25          * 如果元素是0,则不变
26          */
27         public static void Dispose(int[]a)
28         {
29             for (int t = 0; t < a.Length; ++t)
30             {
31                 if (a[t] > 0)
32                 {
33                     a[t] += 1;
34                 }
35                 else if (a[t] < 0)
36                 {
37                     a[t] -= 1;
38                 }
39                 else
40                 {
41                 }
42             }
43 
44                 return;
45         }
46         public static void ArrayOut(int[] a)
47         {
48             for (int t = 0; t < a.Length; ++t)
49             {
50                 Console.Write(a[t]+" ");
51             }
52                 return;
53         }
54     }
55 }
56 
57 输出结果:
58 5 21 9 -2 4 -8 0 11 -3 6
59 6 22 10 -3 5 -9 0 12 -4 7
4
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication12
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         { 
13             String []st = new String[3];
14             st[0] = "";
15             st[1] = "";
16             st[2] = "好人";
17 
18             Program.ArrayStringOut(st);
19             Console.Write("\n");
20 
21             Program.ArrayReverse(st);
22             Program.ArrayStringOut(st);
23 
24             Console.Write("\n");
25             Console.ReadLine();
26         }
27 
28         /// <summary>
29         /// 反转字符串数组
30         /// </summary>
31         /// <param name="st">要反转的字符串</param>
32         static void ArrayReverse(String []st)
33         {
34             for (int i = 0, j = st.Length - 1; i != j; ++i, --j)
35             {
36                 String t = st[i];
37                 st[i] = st[j];
38                 st[j] = t;
39             }
40 
41             return;
42         }
43         /// <summary>
44         /// 输出数组字符串
45         /// </summary>
46         /// <param name="st">要输出的字符串数组</param>
47         static void ArrayStringOut(String []st)
48         {
49             for (int t = 0; t < st.Length; ++t)
50             {
51                 Console.Write(st[t]);
52             }
53 
54                 return;
55         }
56 
57     }
58 }
59 
60 输出结果:
61 我是好人
62 好人是我
5

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

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

例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication7
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int a = GetMax(3,6);
14             Console.WriteLine(a);
15             Console.ReadKey();
16         }
17         /// <summary>
18         /// 计算两个int类型数的最大值
19         /// </summary>
20         /// <param name="n1">第一个整数</param>
21         /// <param name="n2">第二个整数</param>
22         /// <returns>返回两个数中的最大整数</returns>
23         public static int GetMax(int n1, int n2)
24         {
25             return n1 > n2 ? n1 : n2;
26         }
27     }
28 }
代码

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

练习:方法判断是否是闰年

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.Write("请输入要判断的年份:");
14             try
15             {
16                 int year = Convert.ToInt32(Console.ReadLine());
17                 if (Program.Year(year))
18                 {
19                     Console.WriteLine("是闰年!");
20                 }
21                 else
22                 {
23                     Console.WriteLine("不是闰年!");
24                 }
25             }
26             catch(Exception ex)
27             {
28                 Console.WriteLine(ex.Message);
29             }
30             Console.ReadKey();
31         }
32         public static bool Year(int year)
33         {
34             bool b = (0==year%4&&0!=year%100)||0==year%400;
35 
36             return b;
37         }
38     }
39 }
View Code
posted @ 2016-05-09 07:34  静梦亭  阅读(504)  评论(0编辑  收藏  举报