第3章C#基础(5学时).ppt
《第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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 基础 学时
链接地址:https://www.31doc.com/p-3426135.html