版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#入门源代码 part11.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random产生随机数 Array Arr = Array.CreateInstance(typeof(Int32), 10);/创建Array类的一个实例 for (int x = Arr.GetLowerBoun
2、d(0); x < Arr.GetUpperBound(0) + 1; +x) Arr.SetValue(rand.Next() % 100, x); /SetValue()成员方法初始化元素 Console.WriteLine("还未排序的数组元素:"); foreach (int x in Arr) Console.Write(x+" "); int total = 0; Console.WriteLine("rnn这些数组元素的和与均值:"); foreach (int x in Arr) total += x; Cons
3、ole.WriteLine(total+", "+(double )total /(double )Arr.Length ); Array.Sort(Arr ); Console.WriteLine("rn升序排列后的数组元素:"); foreach (int x in Arr) Console.Write(x+" "); Array.Reverse(Arr ); Console.WriteLine("rnrn降序排列后的数组元素:"); foreach (int x in Arr) Console.Write(x
4、+" "); Console.WriteLine (); Console.Read(); 2.using System;public class program public static void Main() int intArr = new int5 10,20,30,40,50; for (int counter = 0; counter < intArr.Length; counter+) Console.WriteLine("intArr"+counter +"="+intArr counter ); char ch
5、arArr = new char 'h','e','l','l','o' for (int counter = 0; counter < charArr.Length; counter+) Console.WriteLine("charArr"+counter +"="+charArr counter ); string strArr = "hello", "csharp" ; foreach (string str in str
6、Arr) Console.WriteLine("strArr="+str); Console.ReadLine(); 3.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random产生随机数 Array Arr = Array.CreateInstance(typeof(
7、Int32 ),10);/创建Array类的一个实例 for (int x = Arr.GetLowerBound(0); x < Arr.GetUpperBound(0) + 1; +x) Arr.SetValue(rand.Next ()%100,x); /SetValue()成员方法初始化元素 int total = 0; Console.Write("Array values are "); foreach (int val in Arr) total += val; Console.Write(val+","); Console.Writ
8、eLine("均值是:0,0:f1",(double )total /(double )Arr.Length );/格式化输出 Console.ReadLine(); 4.using System;namespace nsArray public class program public static void Main() DateTime now = DateTime.Now;/System.DateTime Random rand = new Random(int)now.Millisecond);/System.Random产生随机数 Array iArr = Ar
9、ray.CreateInstance(typeof(Int32), 10);/创建Array类的一个实例 for (int x = iArr.GetLowerBound(0); x < iArr.GetUpperBound(0) + 1; +x) iArr.SetValue(rand.Next() % 100, x); /SetValue()成员方法初始化元素 double dArr=new double 8; Array.Copy(iArr ,dArr,dArr.Length ); Console.Write("dArr数组元素包括:rn "); foreach (
10、double d in dArr) Console.Write("0,4:f1 ",d); Console.Write("rnrn iArr数组元素包括:rn "); foreach (int x in iArr) Console.Write(x+" "); Console.WriteLine(); Console.ReadLine(); 5.using System;namespace ArrIndex public class program static public void Main() int Arr = new int1
11、2 29, 82, 42, 46, 54, 65, 50, 42, 5, 94, 19, 34 ; Console.WriteLine("42第一次出现在下标位置:"+Array.IndexOf (Arr,42); Console.WriteLine("42最后一次出现在下标位置:"+Array.LastIndexOf (Arr,42); int x = 0; while (x = Array.IndexOf(Arr, 42, x) > 0) Console.WriteLine("42在下标" + x + "处出现&q
12、uot;); +x; /+x x = Arr.Length - 1; while (x = Array.LastIndexOf(Arr, 42, x) >= 0) Console.WriteLine("42在下标" + x + "处出现"); -x; /-x Console.ReadLine (); 6.using System;class Star public string name; public int brightness; public Star(string name, int brightness) = name
13、; this.brightness = brightness; public class program public static void Main() Star galaxy = new Star10; galaxy1 = new Star("Sun",3); galaxy2 = new Star("Moon",1); Console.WriteLine("元素个数="+galaxy.Length ); for (int x = 0; x < galaxy.Length; x+) if (galaxyx != null)
14、Console.WriteLine("galaxy"+x+".name:"+ ); Console.WriteLine("galaxy"+x+".brightness="+galaxyx.brightness ); Console.ReadLine(); 7.using System;class Star public string name; public int brightness; public Star(string name, int brightness)
15、= name; this.brightness = brightness; public class program public static void Main() Star, , galaxy = new Star10, 5, 3; galaxy1, 3, 2 = new Star("Sun",3); galaxy4, 1, 2 = new Star("Moon",1); Console.WriteLine("维数="+galaxy.Rank+",元素个数="+galaxy .Length ); for(in
16、t x=0;x<galaxy.GetLength (0);x+) for(int y=0;y<galaxy.GetLength (1);y+) for (int z = 0; z < galaxy.GetLength(2); z+) if (galaxyx, y, z != null) Console.WriteLine("galazy"+x+","+y+","+z+".name="+galaxy x,y, ); Console.WriteLine("galazy"
17、 + x + "," + y + "," + z + ".brightness=" + galaxyx, y, z.brightness); Console.ReadLine(); 8.using System;namespace ArrayTest class Employee private int empID; public Employee(int empID) this.empID = empID; public override string ToString() return empID.ToString(); publ
18、ic class ClassArray public void Run() int intArr; Employee empArr; intArr = new int5; empArr = new Employee3; for (int i = 0; i < empArr.Length; i+) empArri = new Employee(i+5); Console.WriteLine("The int array."); for (int i = 0; i < intArr.Length; i+) Console.WriteLine(intArri.ToSt
19、ring(); Console.WriteLine("nThe employee array."); for (int i = 0; i < empArr.Length; i+) Console.WriteLine(empArri.ToString(); static void Main() ClassArray c = new ClassArray(); c.Run(); Console.ReadLine(); 9.using System;namespace base_ public class Person protected string ssn = &quo
20、t;111-222-333-444" protected string name = "张三" public virtual void GetInfo() Console.WriteLine("姓名:0",name ); Console.WriteLine("编号:0",ssn ); public class Employee : Person public string id = "ABC567" public override void GetInfo() base.GetInfo(); Consol
21、e.WriteLine("成员id:0",id ); class program public static void Main() Employee e = new Employee(); e.GetInfo(); Console.Read();/ 10.using System;namespace whilebreak class program static void Main(string args) for (int i = 0; i < 3; i+) Console.WriteLine("外部循环:"+i); Console.Write
22、("内部循环:"); int t = 0; while (t <100) if (t = 10) break; /t为10时就已经跳出了while循环体,所以参与循环的数压根就没超过10 /所以可写成:while(t<=10). Console.Write(t+" "); t+; Console.WriteLine(); Console.WriteLine("循环结束"); Console.ReadLine(); 11.using System;using System.Collections;using System.C
23、ollections.Generic;namespace Collection class Product string pName; int pNum; string pMaker; string pTime; int pSales; double pPrice; public Product(string _pName, int _pNum, string _pMaker, string _pTime, int _pSales, double _pPrice) pName = _pName; pNum = _pNum; pMaker = _pMaker; pTime = _pTime; p
24、Sales = _pSales; pPrice = _pPrice; public string _pName get return pName; set pName = value; public int _pNum get return pNum; set pNum = value; public string _pMaker get return pMaker; set pMaker = value; public string _pTime get return pTime; set pTime = value; public int _pSales get return pSales
25、; set pSales = value; public double _pPrice get return pPrice; set pPrice = value; class Computer : Product public Computer(string pname, int pnum, string pmaker, string ptime, int psales, double pprice, string type) : base(pname, pnum, pmaker, ptime, psales, pprice) _type = type; public string _typ
26、e get return _type; set public void PlayGames() Console.WriteLine("Play Computer Games!"); class Printer:Product string category; public string _Category get return category; set category = value; public Printer(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice
27、, string cat) : base(pName, pNum, pMaker, pTime, pSales, pPrice) category = cat; public void takePrint() Console.WriteLine("Print PictureS!"); class Camera : Product string focus; public string _focus get return focus; set focus = value; public Camera(string pName, int pNum, string pMaker,
28、 string pTime, int pSale, double pPrice, string foc) : base(pName, pNum, pMaker, pTime, pSale, pPrice) focus = foc; public void takePhoto() Console.WriteLine("Take Photos!"); class Phone : Product string frequency; public string _frequecy get return frequency; set frequency = value; public
29、 Phone(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice, string freq) : base(pName, pNum, pMaker, pTime, pSales, pPrice) frequency = freq; public void takeCall() Console.WriteLine("Take Phonecall!"); class Mp3 : Product string Mp3type; public string _Mp3type g
30、et return Mp3type; set Mp3type = value; public Mp3(string pName, int pNum, string pMaker, string pTime, int pSales, double pPrice, string Mt) : base(pName, pNum, pMaker, pTime, pSales, pPrice) Mp3type = Mt; public void listenMusic() Console.WriteLine("Listen to the Music!"); class program
31、static void Main(string args) Mp3 m1 = new Mp3("翠竹", 100, "深圳恒业", "2007-10-23", 21, 403, "国产"); Mp3 m2 = new Mp3("lopo", 100, "深圳恒业", "2007-10-23", 33, 830, "进口"); Phone p1 = new Phone("摩托罗拉v988", 1000, "深
32、圳创业", "2006-10-23", 233, 1830, "GSM"); Phone p2 = new Phone("摩托罗拉v1902", 1000, "深圳创业", "2006-10-23", 33, 80, "CDMA"); Phone p3 = new Phone("熊猫v3211", 1000, "深圳创业", "2006-3-23", 33, 1230, "CDMA");
33、 Computer c1 = new Computer("宏基3211", 1000, "深圳创业", "2006-3-23", 33, 8230, "笔记本"); Computer c2 = new Computer("宏基TC003", 1000, "深圳创业", "2006-3-23", 33, 4230, "台式机"); ArrayList paList = new ArrayList();/产品数组列表 paList.Add(
34、p1); paList.Add(p2); paList.Add(p3); paList.Add(m1); paList.Add(m2); paList.Add(c1); paList.Add(c2); ArrayList spaList = new ArrayList();/抽样数组列表 spaList.Add(Product)paList1);/变换成Product类型很有必要,否则最后一项属性会不同 spaList.Add(Product)paList2); spaList.Add(Product)paList3); foreach (Product mySample in spaList
35、) Console.WriteLine("采样产品 " + mySample._pName + " 销售数:" + mySample._pSales); Computer c3 = new Computer("IBM Re6", 1000, "联想", "2007-73-23", 33, 8230, "笔记本"); paList.Add(c3); foreach (Product pro in paList) Console.WriteLine("产品 "
36、 + pro._pName + " 总数量:" + pro._pNum); Console.ReadLine(); 12.using System;namespace C_Exception class Ridio public void TurnOn(bool on) if (on) Console.WriteLine("Accelerate."); else Console.WriteLine("Speed is undercontrol."); class Car public const int MaxSpeed = 100;
37、 private int curSpeed; private string petName; private bool carIsDead; private Ridio theMusicBox = new Ridio(); public Car() public Car(string name, int cursp) curSpeed = cursp; petName = name; public void CrankTunes(bool state) theMusicBox.TurnOn(state); public void Accelerate(int delta) if (carIsD
38、ead) Console.WriteLine("0 不能正常工作.", petName); else curSpeed += delta; if (curSpeed < MaxSpeed) Console.WriteLine("0 已经过热了.", petName); curSpeed = 0; carIsDead = true; throw new Exception(string.Format("0 已经太热了.", petName); else Console.WriteLine("=>汽车当前速度为:0&
39、quot;, curSpeed); class program static void Main(string args) Console.WriteLine("=汽车控制程序="); Console.WriteLine("=>创建一个Car,开始加速."); Car mycar = new Car("长安发动机", 20); mycar.CrankTunes(true); try for (int i = 0; i < 10; i+) mycar.Accelerate(10); catch(Exception e) Co
40、nsole.WriteLine("n=ERROR="); Console.WriteLine("方法:0", e.TargetSite); Console.WriteLine("消息:0", e.Message); Console.WriteLine("源码:0", e.Source); Console.Read(); 13.using System;using System.IO;namespace C_File class Customer string custName; string custID; str
41、ing custAccount; public Customer(string id, string name, string account) custName = name; custID = id; custAccount = account; public Customer(string id, string name) custID = id; custName = name; public string _custID get return custID; set custID = value; public string _custName get return custName
42、; set custName = value; public string _custAcount get return custAccount; set custAccount = value; class BankStaff public void Desposit(string custName, string custNum, int money, string staffID) Console.WriteLine("银行业务员" + staffID + "为用户" + custName + "从账户" + custNum +
43、 "存款" + money); public string Register(string staffID, string custName, string custID) Console.WriteLine("银行业务员" + staffID + "为用户" + custName + "注册账户" + "quot;); return "quot; public void UnRegister(string custNa
44、me, string custID, string custNum, string staffID) Console.WriteLine("银行业务员" + staffID + "删除用户" + custName + "的账户:" + custNum); public void FetchMoney(string staffID, string custName, string custNum, int money) Console.WriteLine("银行业务员" + staffID + "为用户&q
45、uot; + custName + "从账户" + custNum + "取款" + money); public void Login(string staffID, string pw) Console.WriteLine("银行职员 " + staffID + "登陆银行系统!"); class LogInfo public LogInfo() File.CreateText("D:My DocumentsC#testtestfile.txt"); public void WriteLog
46、Info(string log) using (StreamWriter writer = new StreamWriter("D:My DocumentsC#testtestlog.txt", true) writer.WriteLine(log); writer.Close(); public void ReadLogInfo() Console.WriteLine("读取日志信息:n"); using (StreamReader sr = new StreamReader ("D:My DocumentsC#testtestlog.txt") string input = null; while (input = sr.ReadLine() != null) Console.WriteLine(input); sr.Close(); class program static void Main(string a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年丹江口工程管理局职工大学马克思主义基本原理概论期末考试模拟试卷
- 2024年济南护理职业学院马克思主义基本原理概论期末考试笔试题库
- 2025年山东商业职业技术学院马克思主义基本原理概论期末考试真题汇编
- 2025年延安职业技术学院马克思主义基本原理概论期末考试笔试真题汇编
- 2025年湖南医药学院马克思主义基本原理概论期末考试真题汇编
- 2025高中秋季化学真题试卷含答案
- 2025春季高三历史冲刺押题
- 民俗文化节策划方案
- 生态湿地修复建设方案
- 企业员工技能培训方案
- 机械点检员职业资格知识考试题及答案
- 2024人形机器人产业半年研究报告
- NB-T20048-2011核电厂建设项目经济评价方法
- 生物医学分析化学方程式总结
- 钯金的选矿工艺
- 家庭系统疗法
- JCT640-2010 顶进施工法用钢筋混凝土排水管
- 四川省遂宁市2024届高三上学期零诊考试高三生物答案
- 桥梁施工技术培训课件
- 南部山区仲宫街道乡村建设规划一张表
- 锅炉焊接工艺规程
评论
0/150
提交评论