第3章C程序设计(5学时自学为主)

上传人:m**** 文档编号:590693807 上传时间:2024-09-15 格式:PPT 页数:100 大小:1.07MB
返回 下载 相关 举报
第3章C程序设计(5学时自学为主)_第1页
第1页 / 共100页
第3章C程序设计(5学时自学为主)_第2页
第2页 / 共100页
第3章C程序设计(5学时自学为主)_第3页
第3页 / 共100页
第3章C程序设计(5学时自学为主)_第4页
第4页 / 共100页
第3章C程序设计(5学时自学为主)_第5页
第5页 / 共100页
点击查看更多>>
资源描述

《第3章C程序设计(5学时自学为主)》由会员分享,可在线阅读,更多相关《第3章C程序设计(5学时自学为主)(100页珍藏版)》请在金锄头文库上搜索。

1、.NET架构架构主讲教师 张智计算机学院.NET架构第3章 C#程序设计nC#是一种面向对象的编程语言。nC#源程序扩展名为.cs,.NET提供的编译器是csc.exe。nC# 语言在类型安全性、版本转换、事件和垃圾回收等方面进行了相当大的改进和创新。.NET架构第3章 C#程序设计3.1 C#程序基本结构3.2 C#数据类型和运算符3.3 C#基本输入输出3.4 C#流程控制语句3.5 C#数组3.6 动态数组ArrayList.NET架构3.1 C#程序基本结构using System;namespace HelloWorld class Program static void Main(

2、string args) System.Console.WriteLine(Hello World);.NET提供许多功能类,以方便应用程序使用,将这些类依照功能分门别类形成命名空间。使用System命名空间定义命名空间HelloWorld(非必须)程序入口定义一个类.NET架构多个类的C#程序using System;namespace HelloWorld /命名空间class TestClass private int a; /成员变量 public TestClass(int x) a=x; /构造函数 public void show() Console.WriteLine(a);

3、/成员函数(方法)class Class1 static void Main(string args) TestClass A=new TestClass(100); /构造对象(实例化) A.show(); .NET架构了解命名空间using System;namespace HelloWorld class TestClass private int a; public TestClass(int x) a = x; public void show() Console.WriteLine(a); namespace HelloWorld2 class Class1 static void

4、 Main(string args) HelloWorld.TestClass A = new HelloWorld.TestClass(100); A.show(); 【返回】命名空间.NET架构3.2 C#数据类型和运算符1. 数据类型和运算符2. 用法示例【返回】.NET架构1. 数据类型和运算符nC#两个基本类别 n值类型 -int、double、char、枚举类型、结构体等-表示实际数据,只是将值存放在内存中 -值类型都存储在堆栈中n引用类型 -类、接口、数组、字符串等-表示指向数据的指针或引用-包含内存堆中对象的地址 -为 null,则表示未引用任何对象 .NET架构类类 别别说说

5、 明明值类型简单类型有符号整型:sbyte, short, int, long无符号整型:byte, ushort, uint, ulongUnicode 字符:charIEEE浮点型:float, double高精度小数:decimal布尔型:bool 枚举类型enum E .结构类型struct S .引用类型类类型所有其他类型的最终基类:objectclass C .接口类型interface I .Unicode字符串string数组类型一维和多维数组,例如 int 和 int , 委托类型delegate T D(.) 形式的用户定义的类型.NET架构类型说明类型说明sbyte有符号

6、8位整数:-128 到 127byte无符号8位整数:0 到 255char16位Unicode字符:0 到 65535short有符号16位整数:-32,768 到 32,767ushort无符号16位整数:0 到 65,535int 有符号32位整数:-2,147,483,648 到 2,147,483,647uint无符号32位整数:0 到 4,294,967,295long 有符号64位整数:-9,223,372,036,854,775,808到9,223,372,036,854,775,807ulong无符号64位整数:0 到 18,446,744,073,709,551,615Un

7、icode是一种通用字符编码标准,它覆盖了多国语言和符号,兼容ASCII字符(前128个相同)。.NET架构类型说明类型说明float 单精度32位浮点数(精度7位):1.5 1045 到 3.41038double 双精度64位浮点数(精度15-16位):5.0 10324 到 1.710308decimal128位数据类型(精度28-29位):1.0 1028到7.91028 (财务/货币)bool 布尔变量(true 或 false),显示时为True或Falsestring 表示一个 Unicode 字符的字符串object可将任何类型的值赋给 object 类型的变量.NET架构n

8、C#运算符运算符类别运算符类别运算符运算符算术+-*/%逻辑(布尔型和按位)&|!&|truefalse字符串串联+递增、递减+-移位关系=!=赋值=+=-=*=/=%=&=|=成员访问.索引 转换( )条件?:委托串联和移除+-间接寻址和地址*-&创建对象new类型信息sizeoftypeof asis溢出异常控制checkedunchecked【返回】.NET架构用法示例数值型static void Main(string args) bool flag = true;short a = 19;int i = (int)3.0;float f = 3.14F; string str = T

9、om;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; .NET架构用法示例数值型static void Main(string args) const float PI = 3.14F; const f

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

11、.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代表宽度,负号表示左对齐.NET架构用法示例bool型基本用法:bool b1 = true; /不

12、是Truebool b2 = ( x=1 & x=5 );示例:if (b2) else int x = 0, y;if (x) y=0; else y=1; 思考结果如何.NET架构注意int x = 0;if (x) 可用 if ( x!= 0 ) 进行显式比较或者用转换函数if (System.Convert.ToBoolean(x) /非0为true无法将类型int隐式转换为bool.NET架构用法示例字符型char ch = A; char ch = 101; / 用8进制数表示ASCII字符,最多3位char ch = x41; /用2位16进制数表示ASCII字符char ch

13、= x0041; /用低2位16进制数表示ASCII字符char ch =u0041; /Unicode字码,必须4位16进制数.NET架构注意问题ch=a; / 无法将int隐式转换为char改正:char ch = (char)a; int a=A; /int a = 65;char ch ;ch = a;a = ch+;Console.WriteLine(a);.NET架构程序练习using System;public class BoolTest public static void Main() Console.Write(Enter a character: ); char c =

14、 Console.Read(); / 读入一个字符(返回值为int型) if ( ) if ( ) Console.WriteLine(小写字母.); else Console.WriteLine(大写字母.); else Console.WriteLine(不是字母.); .NET架构常用方法常用方法说明说明IsControl 字符是否属于控制字符类别。IsDigit 字符是否属于十进制数字类别。IsLetter 字符是否属于字母类别。IsLetterOrDigit 字符是属于字母类别还是属于十进制数字类别。IsLower 字符是否属于小写字母类别。IsNumber 字符是否属于数字类别。I

15、sPunctuation 字符是否属于标点符号类别。IsSeparator 字符是否属于分隔符类别。IsSymbol 字符是否属于符号字符类别。IsUpper 字符是否属于大写字母类别。IsWhiteSpace 字符是否属于空白类别。ToLower 将字符的值转换为它的小写等效项。ToUpper 将字符的值转换为它的大写等效项。用法:char.方法名(参数).NET架构参考答案using System;public class BoolTest public static void Main() Console.Write(Enter a character: ); char c = (cha

16、r) Console.Read(); / 读入一个字符(返回值为int型) if ( char.IsLetter(c) ) if ( char.IsLower(c) ) Console.WriteLine(小写字母.); else Console.WriteLine(大写字母.); else Console.WriteLine(不是字母.); .NET架构用法示例string型string a = hello;string b = h;b += ello; / +是连接字符串, b= hellostring c = good + morning;Console.WriteLine( a !=

17、b ); / 输出结果是False Console.WriteLine( (object)a = b ); /结果是False(类型不一致了).NET架构编程练习n输入一行字符串,统计字母、数字、空格和其他字符个数。.NET架构public static void Main() int letters = 0, digits = 0, spaces = 0, others = 0; Console.WriteLine(请输入一个字符串: ); _; for ( _ ) if ( _ ) letters+; else if ( _ ) digits+; else if ( _ ) spaces+

18、; else others+;Console.WriteLine(字母的个数为: 0,letters);Console.WriteLine(数字的个数为: 0, digits);Console.WriteLine(空格的个数为: 0, spaces); Console.WriteLine(其他字符的个数为: 0, others); .NET架构常用方法常用方法说明说明CompareTo字符串比较:s1.CompareTo(s2) -1(s1s2)Concat 字符串连接( string.Concat(s1,s2) )Contains 返回true|false,指示指定的字符串是否出现在此字符串

19、中。Equals 确定两个字符串是否具有相同的值。IndexOf 获得一个或多个字符在此字符串中的第一个匹配项的索引值。Insert 在字符串中指定索引位置插入一个指定的字符串。LastIndexOf 获得字符或字符串在此字符串中的最后一个匹配项的索引值。Remove 从此字符串中删除指定个数的字符。Replace 将字符串所有匹配项替换为其他指定的字符或串。Substring检索子字符串。s1.Substring(startIndex,length)ToLower 字符串转换为小写形式。ToUpper 字符串转换为大写形式。Trim移除所有前导匹配项和尾部匹配项。.NET架构可以用索引运算符

20、 访问字符串中的各个字符: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+) System.Console.Write( ss.Length - i - 1 );用法示例string型字符串的长度属性.NET架构用法示例string型字符串中可以包含转义符,如:string hello = HellonWorld!;练习:c:myFoldermyFil

21、e.txtstring s1 = c:myFoldermyFile.txt; / string s2 = c:myFoldermyFile.txt; / 不易阅读改进:C#字符串可以开头,并用双引号引起来: string s3 = c:myFoldermyFile.txt; .NET架构注意:若要在一个用 引起来的字符串中包括一个双引号,则应使用两个双引号:例如: You! cried the captain.则用: You! cried the captain.2个双引号才表示1个.NET架构字符串转换为其它型的方法1.Parse() 方法 很重要2.Sytem.Convert.() 方法.

22、NET架构1. .Parse() 方法nint.Parse(string)nlong.Parse(string)nfloat.Parse(string)ndouble.Parse(string)nbool.Parse(string)nchar.Parse(string)nDateTime.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.

23、WriteLine(dt.Year);.NET架构2. Sytem.Convert.() 方法方法描述ToInt32转换为 32 位有符号整数 (int)。ToInt16转换为 16 位有符号整数。ToDouble转换为双精度浮点数字。ToSingle转换为单精度浮点数字。ToUInt32转换为 32 位无符号整数。ToUInt16转换为 16 位无符号整数。ToBoolean转换为等效的布尔值。ToChar转换为 Unicode 字符。ToDateTime将指定的值转换为 DateTime。.NET架构示例string s1=123;int a=System.Convert.ToInt32(

24、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);using System;.NET架构补充n将数字转换为字符串时,需要使用ToString()方法,或者使用Convert.ToString()方法。n例如:int i=123; te

25、xtBox1.Text = Convert.ToString(i); textBox2.Text = i.ToString(); 【返回】.NET架构3.3 C#基本输入输出输出语句:System.Console.Write();System.Console.WriteLine(); /有换行输入语句:System.Console.ReadLine(); /读取一行字符System.Console.Read(); /读取一个字符 (返回值为int型)【返回】.NET架构Console.ReadLine()示例static void Main(string args) Console.Write

26、(请输入姓名:); string a = Console.ReadLine(); Console.WriteLine(欢迎 + a + 光临);连接符.NET架构static void Main(string args) Console.Write(请输入a=); string a = Console.ReadLine(); Console.Write(请输入b=); string b = Console.ReadLine(); Console.WriteLine(a+b= + (a+b) );字符串的连接.NET架构static void Main(string args) Console.

27、Write(请输入a=); string a = Console.ReadLine(); Console.Write(请输入b=); string b = Console.ReadLine(); Console.WriteLine(a+b= + (int.Parse(a)+int.Parse(b) );.NET架构改动一下输出:static void Main(string args) Console.Write(请输入a=); string a = Console.ReadLine(); Console.Write(请输入b=); string b = Console.ReadLine();

28、 int sum=int.Parse(a) + int.Parse(b); Console.WriteLine(0+1=2, a, b, sum);占位符数据用逗号分隔【返回】.NET架构Console.Read()示例static void Main(string args) Console.Write(请输入:); int a = Console.Read(); Console.WriteLine(该字符ASCII码值: + a); Console.WriteLine(该字符是: + (char)a);.NET架构改动一下static void Main(string args) Cons

29、ole.Write(请输入:); char a = (char)Console.Read(); /需强制转换为char型 Console.WriteLine(输入字符: + a); Console.WriteLine(该字符ASCII码值: + (int)a );【返回】.NET架构3.4 C#流程控制语句n选择控制:if、else、switch、casen循环控制:while、do、for、foreachn跳转语句:break、continuen异常处理:try、catch、finally.NET架构foreach循环n用于访问数组或对象集合中的每个元素 (只读)n语法:foreach (数

30、据类型 元素(变量) in 数组或者集合) /语句 n例如:int arr = 1,3,5,2,4,6,8;foreach ( int x in arr ) /x遍历数组元素.NET架构示例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) ; .NET架

31、构示例using System;class Test public static void Main() string array2 = hello, world; foreach (string s in array2) System.Console.WriteLine(s); .NET架构public static void Main() int letters = 0, digits = 0, spaces = 0, others = 0; Console.WriteLine(请输入一个字符串: ); string input = Console.ReadLine(); foreach(

32、char chr in input) if (char.IsLetter(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); .NET架

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

34、类是System.Array。n 一维:类型 数组名=new 类型数组大小n 二维:类型 , 数组名=new 类型行数, 列数n 交错数组 (数组的数组): 例如:int 数组名= new 类型个数 ; 数组下标从0开始【返回】.NET架构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 =

35、 1, 3, 5, 7, 9; /快捷方式.NET架构注意int myArray; /可先声明后初始化myArray = new int 1, 3, 5, 7, 9; / OKint myArray;myArray = 1, 3, 5, 7, 9; / Error.NET架构字符串数组string myStrArr = new string2; /数组初值都为空串string myStrArr = new string Tom, Jerry;string myStrArr = Tom, Jerry;.NET架构数组大小的变量使用int n;n=int.Parse(Console.ReadLin

36、e();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 );int myArr=new intn1,2,3,4,5; 必须用常量.NET架构数组的长度属性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

37、, i, myArri); .NET架构思考数组原来的数据会保留吗?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, myAr

38、ri); .NET架构foreach在数组中的应用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为只读属性.NET架构using System;class MainClass int max,min; void maxmin( ) max=

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

40、Length;i+) if(maxqi)min=qi; public static void Main() int iArray=9,3,18,5,6,1,10,15; maxmin(iArray); Console.WriteLine(最大值:0n最小值:1,max,min); 数组作为函数参数传递.NET架构思考程序问题何在public class MyClass static void maxmin(int q,int a, int b) for (int i = 1; i q.Length; i+) if (a qi) b = qi; public static void Main()

41、 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); .NET架构知识点:关于C#引用ref 关键字:n ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。n 若要使用 ref 参数,则方法定义和调用方法都必须显式使用 ref 关键字。.NET架构示例class RefExample static void

42、Method(ref int i) i = 88; public static void Main() int val = 0; Method(ref val); Console.WriteLine(val); / val is now 88 方法定义和调用都必须显式使用 ref 关键字.NET架构问题解决using System;class MainClass static void maxmin(int q, ref int a, ref int b) for(int i=1;iq.Length;i+) if(aqi)b=qi; static void Main() int iArray

43、= 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); 【返回】.NET架构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; /可先声明后初始化myAr

44、ray = new int , 1,2, 3,4, 5,6, 7,8 ; / OKmyArray = 1,2, 3,4, 5,6, 7,8 ; / Error 1 23 45 67 8视为矩阵要求:列元素个数要一样.NET架构二维数组示例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

45、static void Main() int, myArr=1,2, 3,4, 5,6, 7,8; PrintArray(myArr); .NET架构改进一下using 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 st

46、atic void Main() int, myArr=1,2, 3,4, 5,6, 7,8; PrintArray(myArr); GetLowerBound(int)GetUpperBound(int)返回数组指定维数的下界和上界维数从0开始.NET架构二维数组的属性和方法nGetLowerBound(int)、GetUpperBound(int)返回数组指定维数的下界和上界,维数从0开始nArray.Length 返回Array数组的所有元素的总数nArray.Rank 返回Array数组的秩(维数)nArray.GetLength(int) 返回Array数组的指定维中的元素个数sta

47、tic 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(); .NET架构练习int, myArr = 1, 2 ,3, 3, 4,5 , 5, 6 ,7, 7, 8 ,9 ;则 myArr.Length=?myArr.GetLength(1)=?myArr.Rank=?myArr.GetLowerBound(0)=?【返回】.N

48、ET架构3. 交错数组 交错数组的元素是数组 (数组的数组).交错数组元素的维度和大小可以不同。6235列元素个数可不一样声明方式:int jaggedArr = new int4 ;初始化方式1:jaggedArr0 = new int6 1,3,5,7,9,11 ;jaggedArr1 = new int2 1, 1 ;jaggedArr2 = new int3 2,4,6 ;jaggedArr3 = new int5 1,0,0,0,1;.NET架构交错数组初始化初始化方式2:在声明数组时将其初始化:int jaggedArr = new int4 new int 1,3,5,7,9,1

49、1 , new int 1, 1 , new int 2,4,6 , new int 1,0,0,0,1 ;6235每个元素都是数组new不能少.NET架构交错数组示例using System;public class ArrayClass static void PrintArray(int w) for (int i = 0; i w.Length; i+) Console.Write(第0行: , i); for (int j = 0; j wi.Length; j+) Console.Write(0,-2, wij); Console.WriteLine(); public stati

50、c 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数组指定行中的元素个数.NET架构下面声明和初始化一个一维交错数组,该数组包含大小不同的二维数组元素:int , my

51、JaggedArray = 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【返回】.NET架构3.6 动态数组ArrayListnArrayList类是一个可以动态改变数组长度的特殊数组。n优点n支持自动改变大小的功能n可以灵活的删除、插入元素n提供大量的操作方法n局限性n跟一般的数组比起来,速度上差些.NET架构如何创建ArrayList方式1:ArrayList al1 = new Array

52、List(); /该实例具有默认初始容量,但没有任何元素方式2:ArrayList al2 = new ArrayList(al1);/用已有的集合类来创建新的ArrayList,新老容量相同方式3:ArrayList al3 = new ArrayList(10); /用一个整数值来初始化ArrayList容量添加命名空间:using System.Collections;.NET架构遍历ArrayList数组static void DisplayAL(ArrayList al) if (al.Count = 0) Console.WriteLine(数组没有元素); foreach (ob

53、ject s in al) Console.WriteLine(Convert.ToString(s); for (int i=0; i= 0; i-) int n = ListT.IndexOf(ListTi); if (n = 0 & n != i) ListT.RemoveAt(i); 上海武汉武汉长沙in.NET架构参考2 for (int i = ListT.Count - 1; i = 0; i-) string tempvalue = ListTi.ToString(); ListTi = ; if (ListT.Contains(tempvalue) ListT.RemoveA

54、t(i); else ListTi = tempvalue; .NET架构参考3int n = 0;for (int ii = 0; ii ListT.Count; ii+) for (int jj = ii + 1; jj 0) ListT.RemoveAt(n); jj-; .NET架构参考4:另一种思路string arr= new string 上海, 武汉, 武汉, 长沙;ArrayList ListT = new ArrayList();foreach (string i in arr) if (ListT.IndexOf(i)0) ListT.Add(i); .NET架构 for

55、 (int i = 0; i ListT.Count - 1; i+) for (int j = i + 1; j ListT.Count; j+) if (ListTi = ListTj) ListT.RemoveAt(j); for(int i = 0; i ListT.Count ;i+) if (ListT.IndexOf(ListTi) != ListT.LastIndexOf(ListTi) ListT.Remove(ListTi); ListT.Sort(); int i = 1; while (i ListT.Count) if (ListTi = ListTi - 1) Li

56、stT.RemoveAt(i); else i+; .NET架构编程练习n不用数据库,实现学生对象信息的添加、删除(指定学号)和查询(所有) n学生对象信息如:学号 姓名 成绩 年龄001 张三 72 18002 李四 88 19.NET架构using System;using System.Collections;namespace Student class Program static void Main(string args) String num; String name; String score; String age; String yesOrNo; do Console.W

57、riteLine(添加按1,删除按2,查询按3); switch (Console.ReadLine() case 1: Console.WriteLine(请输入学号:); num = Console.ReadLine(); Console.WriteLine(请输入姓名); name = Console.ReadLine(); Console.WriteLine(请输入成绩); score = Console.ReadLine(); Console.WriteLine(请输入年龄); age = Console.ReadLine(); student students = new stud

58、ent(num, name, score, age); duixiang.Insert(students); /注意调用形式 Console.WriteLine(添加成功); break; case 2: Console.WriteLine(请删除要删除的学号); num=Console.ReadLine(); duixiang.delete(num); break; case 3: duixiang.select(); break; Console.WriteLine(是否继续(Y/N); yesOrNo = Console.ReadLine(); while (yesOrNo = Y |

59、yesOrNo = y); /end of class Program.NET架构 class student String num, name, score, age; public String Age get return age; set age = value; public String Score get return score; set score = value; public String Name get return name; set name = value; public String Num get return num; set num = value; p

60、ublic student(String num, String name, String score, String age) this.num = num; this.name = name; this.score = score; this.age = age; /end of student classclass duixiang /完成Insert(students)功能; /完成delete(num)功能; /完成select(); /end of namespace.NET架构思考题思考题某书店专营IT书籍,希望实现将书籍按着价格从高向低排序,或者将书籍按书名进行排序。Book类

61、:具有三种属性:书名,分类,价格public class Book private string _bookname; /定义书名属性 public string BookName get return _bookname; set _bookname = value; private string _bookcategory; /定义书籍分类属性 public string BookCategory get return _bookcategory; set _bookcategory = value; public double _price; /定义价格属性 public double Price get return _price; set _price = value; public Book(string bookname,string bookcategory,double price) /书籍类的构造函数 _bookname = bookname; _bookcategory = bookcategory; _price = price; 【完】

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

最新文档


当前位置:首页 > 办公文档 > 工作计划

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