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

上传人:sccc 文档编号:4958029 上传时间:2023-05-26 格式:PPT 页数:64 大小:774.54KB
返回 下载 相关 举报
第3章C基础(5学时).ppt_第1页
第1页 / 共64页
第3章C基础(5学时).ppt_第2页
第2页 / 共64页
第3章C基础(5学时).ppt_第3页
第3页 / 共64页
第3章C基础(5学时).ppt_第4页
第4页 / 共64页
第3章C基础(5学时).ppt_第5页
第5页 / 共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(非必须),程序入口,定义一个类,多个类的C#程序,usin

2、g 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 HelloWorld class TestClass

3、 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.数据类型和运算符,C#两个基本类别 值类型 int、double、char、枚举类型、结构

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

5、sole.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(string args)const float PI=3.14F;const float G=980;int length=40;double period=0;period=2*PI*System.Math.Sqrt(l

6、ength/G);Console.WriteLine(钟摆的周期为+period+秒);,符号常量,问题:如何设置输出格式,输出结果:钟摆的周期为1.26875163834526秒,关于输出格式化和多个数据的输出,int i=123;double j=123.45;Console.WriteLine(i,j);Console.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

7、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表示16进制数,4,7代表宽度,负号表示左对齐,用法示例bool型,基本用法:bool b1=true;/小写,不是Truebool 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

8、.Convert.ToBoolean(x)/非0为true,无法将类型int隐式转换为bool,用法示例字符型(2 byte),char ch=A;char ch=101;/用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=(ch

9、ar)i;另:int a=A;,int i=65;char ch=a;ch=i;i=+ch;Console.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(不是字母.);,

10、用法:char.方法名(参数),参考答案,using 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型,strin

11、g a=hello;string b=h;b+=ello;/+是连接字符串,b=hellostring c=good+morning;Console.WriteLine(a!=b);/输出结果是False Console.WriteLine(object)a=b);/结果是False(类型不一致了),可以用索引运算符 访问字符串中的各个字符:char x=test2;/x=s;序号从0开始string s=u0041;char c1=s0,c2=s2;/则c1,c2=?,示例:string s=Printing backwards;/汉字能行吗?for(int i=0;i s.Length;i

12、+)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;/不易阅读改进:C#字符串可以开头,并用双引号引起来:string s3=c:myFoldermyFile.txt;,注意:若要在一个用 引起来的字符串中包括一个双引号,则应使用两个双引号:例如:You

13、!cried the captain.则用:You!cried the captain.,字符串操作API,编程练习,从键盘输入一行字符串,统计字母、数字、空格和其他字符个数。,public static void Main()int letters=0,digits=0,spaces=0,others=0;Console.WriteLine(请输入一个字符串:);string str=Console.ReadLine();/读取一行字符 for(_)if(_)letters+;else if(_)digits+;else if(_)spaces+;else others+;Console.W

14、riteLine(字母的个数为:0,letters);Console.WriteLine(数字的个数为:0,digits);Console.WriteLine(空格的个数为:0,spaces);Console.WriteLine(其他字符的个数为:0,others);,字符串转换为其它型的方法,.Parse()方法 很重要Sytem.Convert.()方法,应用场景:例如将输入的2个整数相加。Console.Write(请输入a=);string a=Console.ReadLine();Console.Write(请输入b=);string b=Console.ReadLine();Con

15、sole.WriteLine(a+b=0,(a+b);,1.Parse()方法,int.Parse(string)long.Parse(string)float.Parse(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

16、);Console.WriteLine(dt.Year);,2.Sytem.Convert.()方法,示例,string s1=123;int a=System.Convert.ToInt32(s1);/a=123string s2=123.45;double f=Convert.ToDouble(s2);/f=123.45string s=true;/不区分大小写bool b=Convert.ToBoolean(s);string s=2008/03/15;/一种日期格式DateTime dt=Convert.ToDateTime(s);Console.WriteLine(dt.Year);

17、,using System;,补充,将数字转换为字符串时,需要使用ToString()方法,或者使用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(数据类型

18、元素(变量)in 数组或者集合)/语句 例如:int 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 static void

19、 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.IsLetter(chr)letters+;else if(char.IsNumber(chr)di

20、gits+;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/执行的代码(可能有异常)。/一旦发现异常,则立即跳到catch执行。catch/处理异常。(若try行代码没有异常,则不执行catch内代码)finally/不管是否有异常都会

21、执行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。一维:类型 数组名=new 类型数组大小 二维:类型,数组名=new 类型行数,列数 交错数组(数组的数组):例如:int 数组名=new 类型个数;,数组下标从0开始,【返回】,1.一维数组,基本用法:int

22、 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 myArray;/可先声明后初始化myArray=new int 1,3,5,7,9;/OKint myArray;myArray=1,3,5,7,9;/Error,字符串数组,string myStrArr=new string2;/数组初值都为空串string mySt

23、rArr=new string Tom,Jerry;string myStrArr=Tom,Jerry;,数组大小的变量使用,int n;n=int.Parse(Console.ReadLine();int myArr=new intn;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

24、;i+)Console.Write(a0=1,-3,i,myArri);,思考数组原来的数据会保留吗?,using System;class Test public 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在数组中的应用

25、,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);,x为只读属性,using System;class MainClass int max,min;void maxmin()max=min=q0;for(int i=1;iqi)min=qi;public static void Main()in

26、t 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);Console.Wri

27、teLine(最大值:0n最小值:1,max,min);,数组作为函数参数传递,数组作为函数参数传递,using System;public class ArrayClassstatic 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,Fri,Sat;PrintArray(WeekDays);,思考程序问题何在,public

28、 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);,知识点:关于C#引用,ref 关键字:ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。若要使用

29、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 关键字,问题解决,using System;class MainClass static void maxmin(int q,ref int a,ref int b)for(int i=1;iqi)b=qi;

30、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=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,

31、3,4,5,6,7,8;/OKmyArray=1,2,3,4,5,6,7,8;/Error,要求:列元素个数要一样,二维数组示例,using System;public class ArrayClass static void PrintArray(int,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);,改进一下,us

32、ing System;public class ArrayClass static void PrintArray(int,w)for(int i=0;i=w.GetUpperBound(0);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)返回数组

33、指定维数的下界和上界维数从0开始,二维数组的属性和方法,GetLowerBound(int)、GetUpperBound(int)返回数组指定维数的下界和上界,维数从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

34、,j,wi,j);Console.WriteLine();,练习,int,myArr=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

35、=new 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+)Console.Write(第0行:,i);

36、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备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号