




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、【精品文檔】如有侵權,請聯系網站刪除,僅供學習與交流c#實例源代碼.精品文檔.【實例1-1】using System;using System.Collections.Generic;using System.Text;namespace _ class Program static void Main(string args) System.Console.WriteLine(恭喜你,學會了C#編程!); System.Console.ReadLine();【實例1-2】private void Form1_Load(object sender, EventArgs e) this.Text
2、=這是一窗口!;Label lbShow = new Label();lbShow.Location = new Point(40,50);lbShow.AutoSize = true;lbShow.Text = 恭喜你學會編程了!;this.Controls.Add(lbShow); int x, y;x = new int5 1,2,3,4,5;y = new int5;y = x;foreach (int a in y)lbShow.Text += a.ToString();this.Controls.Add(lbShow);【實例2-1】using System;using Syste
3、m.Windows.Forms;namespace TestEnum public partial class TestEnum : Form /Visual Studio .Net自動生成的構造函數,后文示例將全部省略 public TestEnum() InitializeComponent(); enum MyEnum a = 101, b, c, d = 201, e, f ; /聲明枚舉型 private void TestEnum_Load(object sender, EventArgs e) MyEnum x = MyEnum.f; /使用枚舉型 MyEnum y = (MyE
4、num)202; string result =枚舉數x的值為; result += (int)x; /將x轉換為整數 result += n枚舉數y代表枚舉元素 + y; lblShow.Text = result;【實例2-2】using System;using System.Windows.Forms;namespace TestStru public partial class TestStru : Form struct Student /聲明結構型 /聲明結構型的數據成員 public int no; public string name; public char sex; pu
5、blic int score; /聲明結構型的方法成員 public string Answer() string result=該學生的信息如下:; result += n學號: + no; /n為換行符 result += n姓名:+ name; result += n性別:+ sex; result += n成績:+ score; return result; /返回結果 private void TestEnum_Load(object sender, EventArgs e) Student s; /使用結構型 s.no = 101; = 黃海; s.sex = 男;
6、s.score = 540; lblShow.Text = s.Answer(); /顯示該生信息 lblShow.Text += nn+DateTime.Now; /顯示當前時間【實例2-3】using System;class TestConstant static void Main(string args) Console.WriteLine(0).GetType(); /有符號的32位整型常量 Console.WriteLine(0U).GetType(); /無符號的32位整型常量 Console.WriteLine(0L).GetType(); /64位的長整型常量 Consol
7、e.WriteLine(0F).GetType(); /32位的浮點型常量 Console.WriteLine(0D).GetType(); /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(); /布爾型常量 Co
8、nsole.WriteLine(u0041).GetType(); /16位的字符型常量Console.ReadLine();【實例2-4】using System;class TestVariable static void Main(string args) int a = 12, b = 15, c, d, e; c = a + b; d = a - b; e = a * b; Console.WriteLine(c=0td=1te=2, c, d, e);【實例2-5】using System;using System.Windows.Forms;namespace TestVaria
9、ble public partial class TestOperator : Form private void 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】using System;using System.Windows.Forms;namespace TestVariable
10、public partial class TestOperator : Form private void 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; /浮點型轉為字符串【實例2-7】using System;using System.Windows.Forms;namespace TestV
11、ariable public partial class TestOperator : Form private void TestVariable_Load(object sender, EventArgs e) int i = 25, j = 12; 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的值為
12、 + (i != j & i = j); lblShow.Text = result;【實例2-8】using System;using System.Windows.Forms;namespace TestInterface public partial class TestInterface : Form interface IStudent /聲明接口 string Answer(); class Student : IStudent /聲明類,以實現接口 public int no; public string name; public string Answer() string r
13、esult = 該學生信息如下:; result += n學號: + no; result += n姓名: + name; return result; private void btnOk_Click(object sender, EventArgs e) Student a = new Student(); /定義并初始化變量a a.no = Convert.ToInt32(txtStuID.Text); = txtName.Text; lblShow.Text = a.Answer();【實例2-9】using System;class HelloWorld public
14、string HelloCN() return 你好!我是Jackson,中國人。; public string HelloEN() return Hi! I am Jackson, a American.;class TestDelegate delegate string MyDelegate(); /聲明委托 static void Main(string args) HelloWorld hello = new HelloWorld(); /創(chuàng)建對象 MyDelegate h = new MyDelegate(hello.HelloCN); /創(chuàng)建委托對象并指向一個方法 Console
15、.WriteLine(h(); /通過委托對象調用所指向的方法 h = new MyDelegate(hello.HelloEN); Console.WriteLine(h();【實例2-10】using System;class TestArray static void Main(string args) int x,y; /聲明數組 x = new int5 1,5,3,2,4; /初始化數組 y = new int5; Array.Copy(x, y, 5); /將數組x的5個元素復制到數組y中 Console.WriteLine(成功地從數組x復制到數組y,數組y各元素值如下:);
16、for (int i = 0; i y.Length; i+) Console.Write(0t, yi); Array.Sort(x); /將數組x的元素排序 Console.WriteLine(n經過排序后,數組x各元素值如下:); for (int i = 0; i x.Length; i+) Console.Write(0t, xi);【實例2-11】using System;using System.Windows.Forms;using System.Text;namespace TestString public partial class TestString : Form p
17、rivate void TestString_Load(object sender, EventArgs e) string s; /定義字符串變量 StringBuilder sb = new StringBuilder(); /創(chuàng)建可變字符串對象 sb.Append(北運); /添加字符串 sb.Insert(1, 京奧); /插入字符串 s = sb.ToString(); /把可變字符串對象轉化為字符串 s = s.Insert(s.Length, 2008); lblShow.Text = + s + 長度為 + s.Length;【實例2-12】using System;using
18、 System.Windows.Forms;namespace TestIf public partial class TestInterface : Form private void btnOk_Click(object sender, EventArgs e) char c = Convert.ToChar(txtChar.Text); /字符串轉換為字符型 if (Char.IsLetter(c) if (Char.IsLower(c) lblShow.Text = 這是一個小寫字母。; else if (Char.IsUpper(c) lblShow.Text =這是大寫字母。; e
19、lse lblShow.Text =這是中文字符。; else lblShow.Text =這不是語言文字。;【實例2-13】using System;class TestSwitch static void 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)
20、 case 1: Console.Write(休閑裝的套數:); s = Console.ReadLine(); t = Convert.ToInt16(s); cost = t * 150; break; case 2: Console.Write(西裝的套數:); s = Console.ReadLine(); t = Convert.ToInt16(s); cost = t * 300; break; case 3: Console.Write(皮衣的件數:); s = Console.ReadLine(); t = Convert.ToInt16(s); cost = t * 600;
21、 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 public partial class TestWhile : Form public TestWhile() /Visual Studio .Net自動生成的構造函數 InitializeC
22、omponent(); private void TestWhile_Load(object sender, EventArgs e) int i,sum; i=1; /循環(huán)變量賦初值 sum=0; while(i= A & c = a & c = z) n+; while (c != n); Console.WriteLine(該行中英文字母的個數為:0, n); 【實例2-16】using System;using System.Windows.Forms;namespace TestFor public partial class TestFor : Form public TestFo
23、r() InitializeComponent(); private void 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; /*百萬富翁第i天給陌生人的錢*/ s1 = s1 + t; /*百萬富翁第i天后共給陌生人的錢*/ s2 = s2 + 100000; /*陌生人第i天后共百萬富翁的錢*/ s1 = s1 / 100; /*將百分富翁給陌生人的分幣換成元*/ MessageBox.Show(百萬富翁給陌生人+s1+元。n陌生人給百萬富翁+s2+元。 );【實例2-17】using System;class TestForeach static void Main() string names = new string5; Console.WriteLine(請輸入五個人的姓名:);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 超市停車協議書模板
- 贈予房屋過戶合同協議
- 財務全職外派合同協議
- 購買服務補充合同協議
- 購買河沙協議書范本
- 2025年酒店管理考核試題及答案
- 第38屆全國中學生物理競賽復賽試題
- 2022年全國中學生數學奧林匹克競賽(預賽)暨2022年全國高中數學聯合競賽加試(B 卷)參考答案及評分標準
- 櫥柜加工安裝合同協議
- 比亞迪合伙協議書模板
- 超構表面透鏡在生物醫(yī)學成像領域應用
- 小水滴的訴說省公開課一等獎新名師優(yōu)質課比賽一等獎課件
- 人體生物醫(yī)學研究倫理審查PPT幻燈片
- 制作自然發(fā)酵酸奶的方法
- 《肖申克的救贖》中英雙語劇本
- 護士長管理能力培訓講義課件
- 2022年黑龍江省鄉(xiāng)村醫(yī)生招聘筆試試題及答案解析
- 濟南市海綿城市建設建筑與小區(qū)改造項目案例-山東省經濟技術開發(fā)中心宿舍-2
- 幼兒園辦學資料:幼兒圖書目錄
- 扣款申請單(標準模版)
- DB31-T 1338-2021船舶供應服務物料產品分類與編碼要求
評論
0/150
提交評論