2022年c#实例源代 .pdf

上传人:Che****ry 文档编号:34262101 上传时间:2022-08-15 格式:PDF 页数:12 大小:91.52KB
返回 下载 相关 举报
2022年c#实例源代 .pdf_第1页
第1页 / 共12页
2022年c#实例源代 .pdf_第2页
第2页 / 共12页
点击查看更多>>
资源描述

《2022年c#实例源代 .pdf》由会员分享,可在线阅读,更多相关《2022年c#实例源代 .pdf(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、【实例 1-1】using System; using System.Collections.Generic; using System.Text; namespace _ classProgram staticvoid Main( string args) System.Console.WriteLine( 恭喜你,学会了 C#编程! ); System.Console.ReadLine(); 【实例 1-2】privatevoid Form1_Load( object sender, EventArgs e) this .Text= 这是一窗口! ; Label lbShow = new

2、Label (); lbShow.Location = new Point (40,50); lbShow.AutoSize = true ; lbShow.Text = 恭喜你学会编程了!; this .Controls.Add(lbShow); int x, y; x = new int 5 1,2,3,4,5; y = new int 5; y = x; foreach ( int a in y) lbShow.Text += a.ToString(); this .Controls.Add(lbShow); 【实例 2-1】using System; using System.Wind

3、ows.Forms; namespace TestEnum 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 12 页 - - - - - - - - - publicpartialclassTestEnum : Form /Visual Studio .Net自动生成的构造函数,后文示例将全部省略public TestEnum() InitializeComponent(); enum MyEnum a = 101, b, c, d = 201, e, f ; / 声明枚

4、举型privatevoid TestEnum_Load( object sender, EventArgs e) MyEnum x = MyEnum .f; / 使用枚举型MyEnum y = (MyEnum )202; string result =枚举数 x的值为 ; result += (int )x; / 将x转换为整数 result += n 枚举数 y代表枚举元素 + y ; lblShow.Text = result; 【实例 2-2】using System; using System.Windows.Forms; namespace Test Stru publicparti

5、alclassTestStru : Form structStudent/ 声明结构型 / 声明结构型的数据成员publicint no; publicstring name; publicchar sex; publicint score; / 声明结构型的方法成员publicstring Answer() string result= 该学生的信息如下:; result += n 学号: + no; /n为换行符 result += n 姓名: + name; result += n 性别: + sex; result += n 成绩: + score; return result; /

6、返回结果名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 12 页 - - - - - - - - - ; privatevoid TestEnum_Load( object sender, EventArgs e) Student s; / 使用结构型 s.no = 101; s.name = 黄海 ; s.sex = 男 ; s.score = 540; lblShow.Text = s.Answer(); / 显示该生信息 lblShow.Text += nn+Dat

7、eTime.Now; /显示当前时间 【实例 2-3】using System; classTestConstant staticvoid Main( string args) Console .WriteLine(0).GetType(); / 有符号的 32位整型常量Console .WriteLine(0U).GetType(); / 无符号的 32位整型常量Console .WriteLine(0L).GetType(); /64 位的长整型常量Console .WriteLine(0F).GetType(); /32 位的浮点型常量Console .WriteLine(0D).Get

8、Type(); /64 位的双精度型常量Console .WriteLine(0M).GetType(); /128 位的小数型常量Console .WriteLine(0 ).GetType(); /16 位的字符型常量Console .WriteLine(0 ).GetType(); / 字符串常量Console .WriteLine(0.0).GetType(); /64 位的双精度型常量Console .WriteLine(true ).GetType(); / 布尔型常量Console .WriteLine(u0041).GetType(); /16 位的字符型常量Console.R

9、eadLine(); 【实例 2-4】using System; classTestVariable staticvoid Main( string args) int a = 12, b = 15, c, d, e; c = a + b; d = a - b; e = a * b; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 12 页 - - - - - - - - - Console .WriteLine(c=0td=1te=2, c, d, e); 【实例 2-

10、5】using System; using System.Windows.Forms; namespace TestVariable publicpartialclassTestOperator : Form privatevoid TestVariable_Load(object sender, EventArgs e) int i = 5, j = 5, p, q; p = (i+) + (i+) + (i+); q = (+j) + (+j) + (+j); string t = ; lblShow.Text = i + t + j + t + p + t + q; 【实例 2-6】us

11、ing System; using System.Windows.Forms; namespace TestVariable publicpartialclassTestOperator : Form privatevoid TestVariable_Load(object sender, EventArgs e) int a, b = 5; char c1 = A ; a = c1; / 字符型转整型float x = 3; x += b; / 整型转浮点型 lblShow.Text = a= + a; / 整型转为字符串 lblShow.Text += nx= + x; / 浮点型转为字符

12、串 【实例 2-7】using System; using System.Windows.Forms; namespace TestVariable 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 12 页 - - - - - - - - - publicpartialclassTestOperator : Form privatevoid TestVariable_Load(object sender, EventArgs e) int i = 25, j = 12;

13、bool k; string result = i!=j的值为 + (i != j); result += n i!=j & i=j的值为 + (i != j & i = j); result += n i!=j & i=j+20的值为 +(i != j & i = j + 20); result += n k = i!=j & i=j的值为 + (i != j & i = j); lblShow.Text = result; 【实例 2-8】using System; using System.Windows.Forms; namespace TestInterface publicpart

14、ialclassTestInterface : Form interfaceIStudent /声明接口 string Answer(); classStudent : IStudent / 声明类,以实现接口 publicint no; publicstring name; publicstring Answer() string result = 该学生信息如下:; result += n 学号: + no; result += n 姓名: + name; return result; privatevoid btnOk_Click(object sender, EventArgs e)

15、Student a = new Student (); /定义并初始化变量a a.no = Convert .ToInt32(txtStuID.Text); a.name = txtName.Text; lblShow.Text = a.Answer(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 12 页 - - - - - - - - - 【实例 2-9】using System; classHelloWorld publicstring HelloCN() r

16、eturn你好!我是 Jackson,中国人。 ; publicstring HelloEN() returnHi! I am Jackson, a American.; classTestDelegate delegatestringMyDelegate (); /声明委托staticvoid Main( string args) HelloWorld hello = new HelloWorld (); /创建对象MyDelegate h = new MyDelegate (hello.HelloCN); /创建委托对象并指向一个方法Console .WriteLine(h(); /通过委

17、托对象调用所指向的方法 h = new MyDelegate (hello.HelloEN); Console .WriteLine(h(); 【实例 2-10】using System; classTestArray staticvoid Main( string args) int x,y; /声明数组 x = new int 5 1,5,3,2,4; /初始化数组 y = new int 5; Array .Copy(x, y, 5); /将数组 x的5个元素复制到数组y中Console .WriteLine( 成功地从数组 x复制到数组 y,数组 y各元素值如下:); for ( in

18、t i = 0; i y.Length; i+) Console .Write(0t, yi); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 12 页 - - - - - - - - - Array .Sort(x); /将数组 x的元素排序Console .WriteLine(n 经过排序后,数组x各元素值如下:); for ( int i = 0; i x.Length; i+) Console .Write(0t, xi); 【实例 2-11】using Sys

19、tem; using System.Windows.Forms; using System.Text; namespace TestString publicpartialclassTestString : Form privatevoid TestString_Load(object sender, EventArgs e) string s; / 定义字符串变量StringBuilder sb = new StringBuilder(); / 创建可变字符串对象 sb.Append( 北运 ); / 添加字符串 sb.Insert(1, 京奥 ); / 插入字符串 s = sb.ToStr

20、ing(); / 把可变字符串对象转化为字符串 s = s.Insert(s.Length, 2008 ); lblShow.Text = + s + 长度为 + s.Length; 【实例 2-12】using System; using System.Windows.Forms; namespace TestIf publicpartialclassTestInterface : Form privatevoid btnOk_Click(object sender, EventArgs e) char c = Convert .ToChar(txtChar.Text); /字符串转换为字符

21、型if ( Char.IsLetter(c) if ( Char.IsLower(c) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 12 页 - - - - - - - - - lblShow.Text = 这是一个小写字母。; elseif ( Char.IsUpper(c) lblShow.Text =这是大写字母。; else lblShow.Text =这是中文字符。; else lblShow.Text = 这不是语言文字。 ; 【实例 2-13】using

22、 System; classTestSwitch staticvoid Main() Console .WriteLine( 服装类别: 1= 休闲装 2=西装 3= 皮衣 ); Console .Write( 请选择类别: ); string s = Console.ReadLine(); int n = Convert .ToInt16(s); /把数字形式的字符串转化为整型数int t,cost = 0; /t用来记录数量,cost 用来记录金额switch (n) case 1: Console .Write( 休闲装的套数: ); s = Console .ReadLine(); t

23、 = Convert .ToInt16(s); cost = t * 150; break ; case 2: Console .Write( 西装的套数: ); s = Console .ReadLine(); t = Convert .ToInt16(s); cost = t * 300; break ; case 3: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 12 页 - - - - - - - - - Console .Write( 皮衣的件数: ); s

24、 = Console .ReadLine(); t = Convert .ToInt16(s); cost = t * 600; break ; default: Console .WriteLine(无效选择,请输入1、2或 3 !); break ; if (cost != 0) Console .WriteLine( 应付款 0 元. , cost); Console .WriteLine( 谢谢您的惠顾! ); 【实例 2-14】using System; using System.Windows.Forms; namespace TestWhile publicpartialclas

25、sTestWhile : Form public TestWhile() /Visual Studio .Net自动生成的构造函数 InitializeComponent(); privatevoid TestWhile_Load(object sender, EventArgs e) int i,sum; i=1; / 循环变量赋初值 sum=0; while (i= A & c = a & c = z) n+; while (c != n); Console .WriteLine( 该行中英文字母的个数为:0 , n); 【实例 2-16】using System; using Syste

26、m.Windows.Forms; namespace TestFor publicpartialclassTestFor : Form public TestFor() InitializeComponent(); privatevoid TestWhile_Load(object sender, EventArgs e) int i; int t; long s1, s2; s1 = t = 1; /* 百万富翁第一天给陌生人的钱为1分*/ s2 = 100000; /* 陌生人第一天给百万富翁的钱为十万元*/ for (i = 2; i = 30; i+) t = t * 2; /* 百万

27、富翁第 i 天给陌生人的钱*/ s1 = s1 + t; /* 百万富翁第 i 天后共给陌生人的钱*/ s2 = s2 + 100000; /* 陌生人第 i 天后共百万富翁的钱*/ s1 = s1 / 100; /* 将百分富翁给陌生人的分币换成元*/ MessageBox .Show( 百万富翁给陌生人+s1+元。 n 陌生人给百万富翁+s2+元。 ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 12 页 - - - - - - - - - 【实例 2-17】

28、using System; classTestForeach staticvoid Main() string names = new string 5; Console .WriteLine( 请输入五个人的姓名: ); for ( int i = 0; i names.Length; i+) namesi=Console .ReadLine(); Console .WriteLine( 已输入的姓名如下,请核对:); foreach ( string name in names) Console .Write(0t, name); 【实例 2-18】using System; classT

29、estForeach staticvoid Main() int i, j, k; for (i = 0; i = 0; j-) /j表示在第 i 行左边的第 j 个空白字符 Console .Write( ); for (k = 0; k 2 * i + 1; k+) /k表示在第 i 行的第 k个星号字符, Console .Write(* ); Console .Write(n ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 12 页 - - - - - -

30、 - - - 【实例 2-21】using System; classTestGoto staticvoid Main() char c; for ( int i=0;i80;i+) /最多输入 80个字符 c=(char ) Console .Read(); if (c= *) break ; /一旦输入星号就结束Console .Write(c); 【实例 2-22】using System; classTestContinue staticvoid Main() char ch_old,ch_new; ch_old=.; Console .WriteLine( 请输入一串字符,以句号结尾: ); do ch_new = (char ) Console.Read(); if (ch_new = ch_old) continue ; Console .Write(ch_new); ch_old=ch_new; while (ch_new!= .); Console .Write(n ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 12 页 - - - - - - - - -

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com