第3章C#基础(5学时课件

上传人:我*** 文档编号:140689187 上传时间:2020-07-31 格式:PPT 页数:64 大小:308.50KB
返回 下载 相关 举报
第3章C#基础(5学时课件_第1页
第1页 / 共64页
第3章C#基础(5学时课件_第2页
第2页 / 共64页
第3章C#基础(5学时课件_第3页
第3页 / 共64页
第3章C#基础(5学时课件_第4页
第4页 / 共64页
第3章C#基础(5学时课件_第5页
第5页 / 共64页
点击查看更多>>
资源描述

《第3章C#基础(5学时课件》由会员分享,可在线阅读,更多相关《第3章C#基础(5学时课件(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#

2、程序,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 Hello

3、World 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(string args) const fl

6、oat 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); Console.WriteLine(i=0,j=

7、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表示16进制数,4, 7代表宽度,负号表示左对齐,用法示例bool型,基本用

8、法: 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; / 用8进制数表示ASCII字符,最多3位 char ch = x4

9、1; /用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; Console.WriteLine(i);,程序练习,using System;

10、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 System; public class BoolTest public stati

11、c 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型,string a = hello; string b = h; b += ello; / +是连接字符

12、串, b= hello string 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.Le

13、ngth; 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; / 不易阅读 改进: C#字符串可以开头,并用双引号引起来: string s3 = c:myFoldermyFile.txt;,注意: 若要在一个

14、用 引起来的字符串中包括一个双引号,则应使用两个双引号: 例如: You! 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 ( _ ) l

15、etters+; else if ( _ ) digits+; else if ( _ ) spaces+; else others+; Console.WriteLine(字母的个数为: 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(); Console.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),s

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号