第3章C#基础(5学时).ppt

上传人:本田雅阁 文档编号:2254240 上传时间:2019-03-11 格式:PPT 页数:64 大小:774.51KB
返回 下载 相关 举报
第3章C#基础(5学时).ppt_第1页
第1页 / 共64页
第3章C#基础(5学时).ppt_第2页
第2页 / 共64页
第3章C#基础(5学时).ppt_第3页
第3页 / 共64页
亲,该文档总共64页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第3章C#基础(5学时).ppt》由会员分享,可在线阅读,更多相关《第3章C#基础(5学时).ppt(64页珍藏版)》请在三一文库上搜索。

1、.NET架构,主讲教师 张智 计算机学院,第3章 C#基础,3.1 C#程序基本结构 3.2 数据类型和运算符 3.3 流程控制语句 3.4 数组,3.1 C#程序基本结构,using System; namespace HelloWorld class Program static void Main(string args) System.Console.WriteLine(“Hello World“); ,.NET提供许多功能类,以方便应用程序使用,将这些类依照功能分门别类形成命名空间。,使用System命名空间,定义命名空间HelloWorld(非必须),程序入口,定义一个类,多个类的

2、C#程序,using System; namespace HelloWorld /命名空间 class TestClass private int a; /成员变量 public TestClass(int x) a=x; /构造函数 public void show() Console.WriteLine(a); /成员函数(方法) class Class1 static void Main(string args) TestClass A=new TestClass(100); /构造对象(实例化) A.show(); ,了解命名空间,using System; namespace Hel

3、loWorld class TestClass private int a; public TestClass(int x) a = x; public void show() Console.WriteLine(a); namespace HelloWorld2 class Class1 static void Main(string args) HelloWorld.TestClass A = new HelloWorld.TestClass(100); A.show(); ,【返回】,命名空间,3.2 数据类型和运算符,1. 数据类型和运算符 2. 用法示例,【返回】,1. 数据类型和运

4、算符,C#两个基本类别 值类型 int、double、char、枚举类型、结构体等 表示实际数据,只是将值存放在内存中 值类型都存储在堆栈中 引用类型 类、接口、数组、字符串等 表示指向数据的指针或引用 包含内存堆中对象的地址 为 null,则表示未引用任何对象,类型说明,Unicode是一种通用字符编码标准,它覆盖了多国语言和符号,兼容ASCII字符(前128个相同)。,类型说明,C#运算符,【返回】,用法示例数值型,static void Main(string args) bool flag = true; short a = 19; int i = (int)3.0; float f

5、= 3.14F; string str = “Tom“; Console.WriteLine (“布尔值 = “ + flag ); Console.WriteLine (“短整型值 = “ + a ); Console.WriteLine (“整型值 = “ + i ); Console.WriteLine (“浮点值 = “ + f); Console.WriteLine (“字符串值 = “ + str ); ,int i=3.0; float f=3.14; double f=3.14; float f=(float)3.14; ,用法示例数值型,static void Main(st

6、ring args) const float PI = 3.14F; const float G = 980; int length = 40; double period = 0; period = 2 * PI * System.Math.Sqrt( length / G ); Console.WriteLine(“钟摆的周期为“+period+“秒“); ,符号常量,问题:如何设置输出格式,输出结果: 钟摆的周期为1.26875163834526秒,关于输出格式化和多个数据的输出,int i=123; double j=123.45; Console.WriteLine(i, j); C

7、onsole.WriteLine(“i=0,j=1“,i,j); /结果 i=123,j=123.45 Console.WriteLine(“i=0,4,j=1,-7“,i,j); /结果 i= 123,j=123.45 Console.WriteLine(“j=0,7:f1“, j); /j=123.5 Console.WriteLine(“j=0:f1“, j); /j=123.5 / f表示浮点数,1表示小数位数(四舍五入) Console.WriteLine(“i=0,7:x“, i); /i=7b Console.WriteLine(“i=0:x“, i); /i=7b / x表示1

8、6进制数,4, 7代表宽度,负号表示左对齐,用法示例bool型,基本用法: bool b1 = true; /小写,不是True bool b2 = ( x=1 示例: if (b2) else ,int x = 0, y; if (x) y=0; else y=1; ,思考 结果如何,注意,int x = 0; if (x) 可用 if ( x!= 0 ) 进行显式比较 或者用转换函数 if (System.Convert.ToBoolean(x) /非0为true,无法将类型int 隐式转换为bool,用法示例字符型 (2 byte),char ch = A; char ch = 101;

9、 / 用8进制数表示ASCII字符,最多3位 char ch = x41; /用2位16进制数表示ASCII字符 char ch = x0041; /用低2位16进制数表示ASCII字符 char ch =u0041; /Unicode字码,必须4位16进制数,Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案。兼容ASCII字符(前128个相同)。,注意问题,ch = i; 无法将int隐式转换为char 改正:char ch = (char) i ; 另: int a=A; ,int i = 65; char ch=a; ch = i; i = +ch; Consol

10、e.WriteLine(i);,程序练习,using System; public class BoolTest public static void Main() Console.Write(“Enter a character: “); char c = Console.Read(); / 读入一个字符(返回值为int型) if ( ) if ( ) Console.WriteLine(“小写字母.“); else Console.WriteLine(“大写字母.“); else Console.WriteLine(“不是字母.“); ,用法:char.方法名(参数),参考答案,using

11、 System; public class BoolTest public static void Main() Console.Write(“Enter a character: “); char c = (char) Console.Read(); / 读入一个字符(返回值为int型) if ( char.IsLetter(c) ) if ( char.IsLower(c) ) Console.WriteLine(“小写字母.“); else Console.WriteLine(“大写字母.“); else Console.WriteLine(“不是字母.“); ,用法示例string型,

12、string a = “hello“; string b = “h“; b += “ello“; / +是连接字符串, b= “hello“ string c = “good “ + “morning“; Console.WriteLine( a != b ); / 输出结果是False Console.WriteLine( (object)a = b ); /结果是False(类型不一致了),可以用索引运算符 访问字符串中的各个字符: char x = “test“2; / x = s; 序号从0开始 string s = “u0041“; char c1=s0, c2=s2; / 则c1,

13、c2=?,示例: string s=“Printing backwards“; /汉字能行吗? for (int i = 0; i s.Length; i+) System.Console.Write( ss.Length - i - 1 ); ,用法示例string型,字符串的长度属性,用法示例string型,字符串中可以包含转义符,如: string hello = “HellonWorld!“; 练习: c:myFoldermyFile.txt,string s1 = “c:myFoldermyFile.txt“; / string s2 = “c:myFoldermyFile.txt“

14、; / 不易阅读 改进: C#字符串可以开头,并用双引号引起来: string s3 = “c:myFoldermyFile.txt“;,注意: 若要在一个用 引起来的字符串中包括一个双引号,则应使用两个双引号: 例如: “You!“ cried the captain. 则用: “You!“ cried the captain.“,字符串操作API,编程练习,从键盘输入一行字符串,统计字母、数字、空格和其他字符个数。,public static void Main() int letters = 0, digits = 0, spaces = 0, others = 0; Console.W

15、riteLine(“请输入一个字符串: “); string str = Console.ReadLine(); /读取一行字符 for ( _ ) if ( _ ) letters+; else if ( _ ) digits+; else if ( _ ) spaces+; else others+; Console.WriteLine(“字母的个数为: 0“,letters); Console.WriteLine(“数字的个数为: 0“, digits); Console.WriteLine(“空格的个数为: 0“, spaces); Console.WriteLine(“其他字符的个数

16、为: 0“, others); ,字符串转换为其它型的方法,.Parse() 方法 很重要 Sytem.Convert.() 方法,应用场景: 例如将输入的2个整数相加。 Console.Write(“请输入a=“); string a = Console.ReadLine(); Console.Write(“请输入b=“); string b = Console.ReadLine(); Console.WriteLine( “ a+b= 0 “, (a+b) );,1. .Parse() 方法,int.Parse(string) long.Parse(string) float.Parse(

17、string) double.Parse(string) bool.Parse(string) char.Parse(string) DateTime.Parse(string),string s1=“123“; int a=int.Parse(s1); string s2 =“123.45“; double f=double.Parse(s2); string s3 = “2008/03/15“; DateTime dt = DateTime.Parse(s3); Console.WriteLine(dt.Year);,2. Sytem.Convert.() 方法,示例,string s1=

18、“123“; int a=System.Convert.ToInt32(s1); /a=123 string s2=“123.45“; double f=Convert.ToDouble(s2); /f=123.45 string s=“true“; / 不区分大小写 bool b=Convert.ToBoolean(s); string s=“2008/03/15“; /一种日期格式 DateTime dt=Convert.ToDateTime(s); Console.WriteLine(dt.Year);,using System;,补充,将数字转换为字符串时,需要使用ToString()

19、方法,或者使用Convert.ToString()方法。 例如: int i=123; TextBox1.Text = Convert.ToString(i); TextBox2.Text = i.ToString();,【返回】,3.3 流程控制语句,选择控制:if、else、switch、case 循环控制:while、do、for、foreach 跳转语句:break、continue 异常处理:try、catch、finally,foreach循环,用于访问数组或对象集合中的每个元素 (只读) 语法: foreach (数据类型 元素(变量) in 数组或者集合) /语句 例如: in

20、t arr = 1,3,5,2,4,6,8; foreach ( int x in arr ) /x遍历数组元素,示例,using System; class Test public static void Main() int odd = 0, even = 0; int arr = 1,3,5,2,4,6,8; foreach (int x in arr) if (x%2 = 0) even+; else odd+; Console.WriteLine(“奇数个数=0, 偶数个数=1 “, odd, even) ; ,示例,using System; class Test public s

21、tatic void Main() string array2 = “hello“, “world“; foreach (string s in array2) System.Console.WriteLine(s); ,public static void Main() int letters = 0, digits = 0, spaces = 0, others = 0; Console.WriteLine(“请输入一个字符串: “); string input = Console.ReadLine(); foreach(char chr in input) if (char.IsLett

22、er(chr) letters+; else if (char.IsNumber(chr) digits+; else if (char.IsWhiteSpace(chr) spaces+; else others+; Console.WriteLine(“字母的个数为: 0“,letters); Console.WriteLine(“数字的个数为: 0“, digits); Console.WriteLine(“空格的个数为: 0“, spaces); Console.WriteLine(“其他字符的个数为: 0“, others); ,异常处理语句,try /执行的代码(可能有异常)。 /

23、一旦发现异常,则立即跳到catch执行。 catch /处理异常。(若try行代码没有异常,则不执行catch内代码) finally /不管是否有异常都会执行finally,包括catch 里面用了return。 ,示例,Console.Write(“请输入a=“); string a = Console.ReadLine(); try int sum = int.Parse(a); Console.Write(sum); catch Console.Write(“请输入整数!“); ,【返回】,3.4 数组,C#数组属于引用类型,其基类是System.Array。 一维:类型 数组名=ne

24、w 类型数组大小 二维:类型 , 数组名=new 类型行数, 列数 交错数组 (数组的数组): 例如:int 数组名= new 类型个数 ;,数组下标从0开始,【返回】,1. 一维数组,基本用法: int myArr = new int 5; /此时元素初值都为零 int myArr = new int51, 3, 5, 7, 9; /自定义数据 int myArr = new int61, 3, 5, 7, 9; 个数不一致 int myArr = new int 1, 3, 5, 7, 9; /数组大小可省略 int myArr = 1, 3, 5, 7, 9; /快捷方式,注意,int

25、myArray; /可先声明后初始化 myArray = new int 1, 3, 5, 7, 9; / OK int myArray; myArray = 1, 3, 5, 7, 9; / Error,字符串数组,string myStrArr = new string2; /数组初值都为空串 string myStrArr = new string “Tom“, “Jerry“; string myStrArr = “Tom“, “Jerry“;,数组大小的变量使用,int n; n=int.Parse(Console.ReadLine(); int myArr = new intn;

26、for (int i = 0; i n; i+) myArri = i+1; for (int i = 0; i n; i+) Console.Write( “0,5:d“, myArri );,数组的长度属性,using System; class Test public static void Main() int myArr = 1, 3, 5, 7, 9; for (int i = 0; i myArr.Length; i+) Console.Write(“a0=1,-3“, i, myArri); ,思考数组原来的数据会保留吗?,using System; class Test pu

27、blic static void Main() int n=5; int myArr = new intn; for (int i = 0; i myArr.Length; i+) myArri = i+1; myArr = new intn + 5; for (int i = 5; i myArr.Length; i+) myArri = i + 1; for (int i = 0; i myArr.Length; i+) Console.Write(“0,5:d“, myArri); ,foreach在数组中的应用,using System; class Test public stati

28、c void Main() int odd = 0, even = 0; int arr = 1,3,5,2,4,6,8; foreach ( int x in arr ) if (x%2 = 0) even+; else odd+; Console.WriteLine(“奇数个数=0, 偶数个数=1 “, odd, even) ; ,x为只读属性,using System; class MainClass int max,min; void maxmin( ) max=min=q0; for(int i=1;iqi)min=qi; public static void Main() int

29、iArray=9,3,18,5,6,1,10,15; maxmin(iArray); Console.WriteLine(“最大值:0n最小值:1“,max,min); ,数组作为函数参数传递完善程序,using System; class MainClass static int max,min; /static整个类中共享 static void maxmin(int q) max=min=q0; for(int i=1;iqi)min=qi; public static void Main() int iArray=9,3,18,5,6,1,10,15; maxmin(iArray);

30、Console.WriteLine(“最大值:0n最小值:1“,max,min); ,数组作为函数参数传递,数组作为函数参数传递,using System; public class ArrayClass static void PrintArray(string w) foreach (string str in w) Console.Write(str + “ “); Console.WriteLine(); public static void Main() string WeekDays = new string “Sun“, “Mon“, “Tue“, “Wed“, “Thu“, “

31、Fri“, “Sat“; PrintArray(WeekDays); ,思考程序问题何在,public class MyClass static void maxmin(int q,int a, int b) for (int i = 1; i qi) b = qi; public static void Main() int iArray = 9, 3, 18, 5, 6, 1, 10, 15 ; int max, min; max = min=iArray0; maxmin(iArray,max,min); Console.WriteLine(“最大值:0n最小值:1“, max, min

32、); ,知识点:关于C#引用,ref 关键字: ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。 若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。,示例,class RefExample static void Method(ref int i) i = 88; public static void Main() int val = 0; Method(ref val); Console.WriteLine(val); / val is now 88 ,方法定义和调用都必须显式使用 ref 关键字,问题

33、解决,using System; class MainClass static void maxmin(int q, ref int a, ref int b) for(int i=1;iqi)b=qi; static void Main() int iArray = 9, 3, 18, 5, 6, 1, 10, 15 ; int max, min; max=min=iArray0; maxmin(iArray, ref max, ref min); Console.WriteLine(“最大值:0n最小值:1“,max,min); ,【返回】,2. 二维数组,int , myArray =

34、new int4,2; /此时元素初值都为零 int , myArray = new int4,2 1,2, 3,4, 5,6, 7,8 ; int , myArray = 1,2, 3,4, 5,6, 7,8; int , myArray; /可先声明后初始化 myArray = new int , 1,2, 3,4, 5,6, 7,8 ; / OK myArray = 1,2, 3,4, 5,6, 7,8 ; / Error,要求:列元素个数要一样,二维数组示例,using System; public class ArrayClass static void PrintArray(int

35、, w) for (int i=0; i 4; i+) for (int j=0; j 2; j+) Console.Write(“a0,1=2,-5“, i, j, wi,j); Console.WriteLine(); public static void Main() int, myArr=1,2, 3,4, 5,6, 7,8; PrintArray(myArr); ,改进一下,using System; public class ArrayClass static void PrintArray(int, w) for (int i=0; i = w.GetUpperBound(0);

36、 i+) for (int j=0; j = w.GetUpperBound(1); j+) Console.Write(“a0,1=2,-5“, i, j, wi,j); Console.WriteLine(); public static void Main() int, myArr=1,2, 3,4, 5,6, 7,8; PrintArray(myArr); ,GetLowerBound(int) GetUpperBound(int) 返回数组指定维数的下界和上界 维数从0开始,二维数组的属性和方法,GetLowerBound(int)、GetUpperBound(int) 返回数组指定

37、维数的下界和上界,维数从0开始 Array.Length 返回Array数组的所有元素的总数 Array.Rank 返回Array数组的秩(维数) Array.GetLength(int) 返回Array数组的指定维中的元素个数,static void PrintArray(int, w) for (int i = 0; i w.GetLength(0) ; i+) for (int j = 0; j w.GetLength(1); j+) Console.Write(“a0,1=2,-5“, i, j, wi, j); Console.WriteLine(); ,练习,int, myArr

38、= 1, 2 ,3, 3, 4,5 , 5, 6 ,7, 7, 8 ,9 ; 则 myArr.Length=? myArr.GetLength(1)=? myArr.Rank=? myArr.GetLowerBound(0)=?,【返回】,3. 交错数组,交错数组的元素是数组 (“数组的数组“). 交错数组元素的维度和大小可以不同。,列元素个数可不一样,声明方式: int jaggedArr = new int4 ; 初始化方式1: jaggedArr0 = new int6 1,3,5,7,9,11 ; jaggedArr1 = new int2 1, 1 ; jaggedArr2 = ne

39、w int3 2,4,6 ; jaggedArr3 = new int5 1,0,0,0,1;,交错数组初始化,初始化方式2: 在声明数组时将其初始化: int jaggedArr = new int4 new int 1,3,5,7,9,11 , new int 1, 1 , new int 2,4,6 , new int 1,0,0,0,1 ;,每个元素都是数组,new不能少,交错数组示例,using System; public class ArrayClass static void PrintArray(int w) for (int i = 0; i w.Length; i+) C

40、onsole.Write(“第0行: “, i); for (int j = 0; j wi.Length; j+) Console.Write(“0,-2“, wij); Console.WriteLine(); public static void Main() int jaggedArr = new int4 ; jaggedArr0 = new int 1, 3, 5, 7, 9, 11 ; jaggedArr1 = new int 1, 1 ; jaggedArr2 = new int 2, 4, 6 ; jaggedArr3 = new int 1, 0, 0, 0, 1 ; PrintArray(jaggedArr); ,交错数组的属性: w.Length:返回w数组的行数 wint.Length:返回w数组指定行中的元素个数,下面声明和初始化一个一维交错数组,该数组包含大小不同的二维数组元素: int , myJaggedArray = new int 3, new int, 1,3, 5,7 , new int, 0,2, 4,6, 8,10 , new int, 11,22, 99,88, 0,9 ; 问题:myJaggedArray11,0 myJaggedArray21,1,【完】,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1