C#基础实例

上传人:小*** 文档编号:146727950 上传时间:2022-08-31 格式:DOCX 页数:73 大小:179.22KB
返回 下载 相关 举报
C#基础实例_第1页
第1页 / 共73页
C#基础实例_第2页
第2页 / 共73页
C#基础实例_第3页
第3页 / 共73页
点击查看更多>>
资源描述
基础知识复习练习题1. 纺写一段程序,运行时向用提问“你考了多少分?(0100)”接受输入后判断其等级并显示出 来。判断依据如下:等则 优(90100);良(8089);中(7079);下6069;差(059)2. 编程输出九九乘法表。3. 定义长度50的数组随机给数组赋值,并可以让用户输入一个数字 n,按一行n个数输出数组 int array=new int50; Random r=new Random(); r.Next(); (注:这道题有点问题如果用户输入 的数大于10将无法按按用户输入的数进行输出一行多少个)4. 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命 名规范 骆驼命名)name s.SubString(0,1) s.SubString(1)5. 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成大小然后返回(命 名规范 帕斯卡)6. 声明两个变量:int n1=10,n2=20; 要求将两个变量交换,最后输出 n1为20,n2为10.扩展(*): 不使用桃花汛三个变量如何交换?7. 用方法来实现:将上题封装一个方法来做。提示:方法有两个参数 n1,n2, 在方法中将n1,n2 进行 交换使用ref8. 请用户输入一个字符串,计算字符串中的字符个数,并输出。9. 用方法来实现:计算两个数的最大值,思考:方法的参数?返回值?扩展:计算任意多个数间的 最大值(提示:params )10. 用方法来实现:计算 1100之间的所有整数的和。11. 用方法来实现:计算 1100之间的所有奇数的和。12. 用方法来实现:判断一个给定的整数是否为“质数”。13. 用方法来实现:计算 1100之间的所有质数(素数)的和。14. 用方法来实现:有一个字符串数组:“马龙”,“迈克尔乔丹”,“雷吉米勒”,“蒂姆邓肯”, “科比布莱恩特”,请输出最长的字符串15. 用方法来实现:请计算出一个整型数组的平均值。 1,3,4,5,93,22,33,6,8,10 要求如 果计算结果有小数,则显示小数点后两位(四舍五入)16. 请通过冒泡排序法对整数数组 1,3,4,5,93,22,33,6,8,10实现升序排序17. 为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一 个1100 的随机值,然后计算平均成绩。18. 有如下字符串:“患者:大夫,我咳嗽得很重”“大夫,你多大年纪”患者:七十五岁,大夫: 那现在不咳嗽,还要等到什么时咳嗽?需求:请统计出该字符中,咳嗽二字的出现次数,以及每 次咳嗽出现的索引位置。/1.用方法来实现:有一个字符串数组:/马龙,迈克尔乔丹,雷吉米勒, 蒂姆邓肯, 科比布莱恩特,请输出最长的using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 方法练习class Programstatic void Main(string args)string names = 马龙, 迈克尔乔丹, 雷吉米勒, 蒂姆邓肯, 科比布莱恩特 ;string max = GetLongest(names);Console.WriteLine(max);Console.ReadKey();public static string GetLongest(string s)string max = s0;for(int i=0;imax.Length) max = si; return max;/2.写一个方法,用来判断用户输入的数字是不是质数/再写一个方法,要求用户只能输入数字 输入有误就一直让用户输入using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 方法练习class Programstatic void Main(string args)while(true)Console.WriteLine( 请输入一个数字,我们将判断你输入的数字是否为质数 ); string strNumber = Console .ReadLine();int number = GetNumber(strNumber);bool b = IsPrime(number);Console.WriteLine(b);Console.ReadKey();public static bool IsPrime(int number)if (number 2) return false; elsefor (int i = 2; i =90)level =优;else if(sum=80)level =良;else if(sum=70)level =中;else if (sum=60)return level = 差;else if(sum60)level =不及格;return level;/4.请将字符串数组“ 中国”,“美国”,“巴西”,“澳大利亚”,“加拿大” 中的元素反转输 出using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 方法练习1class Programstatic void Main(string args)string names = 中国,美国,巴西,澳大利亚,加拿大; Test(names);for(int i=0;inames.Length;i+)Console.Write( namesi+ ,);Console.ReadKey();public static void Test(string names)for(int i=0;inames.Length/2;i+)string temp = namesi;namesi = namesnames.Length - 1 - i;namesnames.Length - i - 1 = temp;/5.循环录入5个人的年龄并计算平均年龄,/如果录入的数据出现负数或大于 100的数,立即停止输入并报错。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习2class Programstatic void Main(string args)int sum = 0;bool b = true;for (int i = 0; i = 0 & age = 100)sum += age;/sum=sum+age;elseConsole.WriteLine(输入的年龄不在正确范围内,程序退出!); b = false;break;if (b)Console.WriteLine(5个人的平均年龄是0 , sum / 5);Console.ReadKey();/6.在while中用break实现要求用户一直输入用户名和密码,只要不是 admin、888888 /就一直提示要求重新输入,如果正确则提示登录成功using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习2class Programstatic void Main(string args)string name= ;string pwd= ;while (true)Console.WriteLine(请输入用户名);name = Console.ReadLine();Console.WriteLine(请输入密码);pwd = Console.ReadLine();if (name = admin & pwd = 888888)Console.WriteLine(登录成功);break;elseConsole.WriteLine(用户名或密码错误,请重新输入); Console.ReadKey();/7. 1100之间的整数相加得到累加值大于 20的当前数/(比如:1+2+3+4+5+621)结果6 sum=20 iusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习2class Programstatic void Main(string args)int sum = 0;for (int i = 0; i 20)Console.WriteLine(加到0的时候结果大于 20, i); break;Console.ReadKey();/8.找出100内的所有的素数/素数/质数:只能被1和这个数字本身整除的数字using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习3class Programstatic void Main(string args)for (int i = 2; i = 100; i+)bool b = true;for (int j = 2; j i; j+)if (i % j = 0)b = false;break;if (b)Console.WriteLine(i);Console.ReadKey();/9.找出100内的所有的素数/素数/质数:只能被1和这个数字本身整除的数字using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习3class Programstatic void Main(string args)for (int i = 100; i 999; i+)int bai = i / 100;int shi = i % 100 / 10;int ge = i % 10;if (bai * bai * bai + shi * shi * shi + ge * ge * ge = i) Console.WriteLine(水仙花数有0, i);Console.ReadKey();/10.在屏幕中输出三角形乘法口决表using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习3class Programstatic void Main(string args)for (int i=1;i=9;i+)for (int j=1;j=i;j+)Console.Write(0*1=2t, i, j, i * j);Console.WriteLine();Console.ReadKey();/11.定义人这个类,并实例化/对象实例一using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习4class Programstatic void Main(string args)Person SuQuan = new Person();SuQuan._name = 孙全;SuQuan._age = 23;SuQuan._gender = 男;SuQuan.CHLSS();Console.ReadLine();/对象实例一public class Personpublic string _name;public int _age;public char _gender;public void CHLSS()Console.WriteLine(我叫0,我今年1,我是 2生,我可以吃喝拉撒睡, this._name,this._age, this._gender);/对象实例二/对实例化的对象设置属性读写权限/属性的作用:保护字段对字段的赋值和取值作限定/set设置value的值且用于初始化字段时; get设置字段的值且用于取值时 public class Personprivate string _name;public string Namegetreturn _name;set_name = value;private int _age;public int Agegetreturn _age;setif (value 100)value = 0;_age = value;private char _gender;public char Gendergetif (_gender != 男 & _gender != 女 )return _gender = 男;return _gender;set_gender = value;public void CHLSS()Console.WriteLine(我叫0,我今年1,我是 2生,我可以吃喝拉撒睡,this._name,this._age,this._gender);/12.写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值) /不能为负数,有一个价格属性,价格属性只读,/并且根据距离 distance计算价格price(1元/公里):/0-100公里/101-200公里/201-300公里 /300公里以上票价不打折 总额打9.5折 总额打9折 总额打8折using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习4class Ticketprivate double _distance;public double Distanceget return _distance; public Ticket(double distance)if (distance 0 & _distance 101 & _distance 201 & _distance = 300)return _distance * 0.9;elsereturn _distance * 0.8;public void ShowTicket()Console.WriteLine(0公里需要1元, Distance,Price); using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习4class Programstatic void Main(string args)Ticket t = new Ticket(90);t.ShowTicket();Console.ReadKey();/13.创建人这个类,含有三个对象分别是姓名,年龄,性别, /创建人这个类的结构函数,/如果年龄小于0,并且大于 100,输出年龄为0,/如果性别不是男也不是女默认为男,/如果姓名不是孙全,默认为孙全using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习4public class Personprivate string name;private int age;private char gender;public string Namegetreturn name;setif (value != 孙全)value = 孙全;name=value;public int Agegetif(age100)return age=0;return age;setage=value;public char Gendergetreturn gender;setgender = value;public void SayHello()Console.WriteLine(0,1,2, this.Name, this.Age, this.Gender); public Person(string name,int age,char gender)this.Name = name;this.Age = age;if (gender != 男 & gender != 女) gender = 男 ; this.Gender = gender;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 练习4class Programstatic void Main(string args)Person zsPerson = new Person(张三, -18, 中);zsPerson.SayHello();Console.ReadKey();/14.从日期字符串(2008-08-08)中分析出年、月、日; 2008年08月08日。/让用户输入一个日期格式如: 2008-08-08 ,输出你输入的日期为 2008年08月08日。 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串练习1class Programstatic void Main(string args)Console.WriteLine(请输入一个日期格式如:(2008-01-01));string s = Console.ReadLine();string str = s.Split(new char - , StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(0年1月2日, str0, str1, str2);Console.ReadKey();/15.创建一个记时器,用来记录程序运行的时间using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 函数class Programstatic void Main(string args)StringBuilder jsq = new StringBuilder();/string str = null;Stopwatch sw = new Stopwatch();/创建一个计时器,用来计录程序运行时间 sw.Start();/开始计时for (int i = 0; i 100000; i+)/str += i;jsq.Append(i);sw.Stop(); /结束计时Console.WriteLine(jsq.ToString();Console.WriteLine(sw.Elapsed);Console.ReadKey();/16.输入一个你想到的名字/输出这个名字的长度using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 函数class Programstatic void Main(string args)Console.WriteLine(请输入一个你想到的名字:); string s = Console.ReadLine();Console.WriteLine(名字的长度是0, s.Length); Console.ReadKey();/17.判断两个学员输入各自最喜欢的课程名称,判断是否一致,/如果相等,输出你们喜欢相同的课程,如果不相同,/则输出你们俩喜欢不相同的课程using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 函数class Programstatic void Main(string args)Console.WriteLine(请输入你喜欢的课程:); string studentone = Console.ReadLine(); /studentone = studentone.ToUpper(); Console.WriteLine(请输入你喜欢的课程); string studenttow = Console.ReadLine();/ studenttow = studenttow.ToUpper();if (studentone.ToUpper() = studenttow.ToUpper() /把接收到的字符串都转换成大写进行比较/if(studentone.ToLower()=studenttow.ToLower()/把接收到的字符串都转换成小写进行比较Console.WriteLine(喜欢的课程是一样的0, studentone);elseConsole.WriteLine(喜欢的课程不一样0,1,studentone,studenttow); Console.ReadKey();/18.查找目录下的苍老师.wav文件using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string path = c:adasdsfdfdv苍老师.wav;int index=path.LastIndexOf();path = path.Substring(index+1);Console.WriteLine(path);Console.ReadKey();/19.替换文本中的文字using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string str = 胡长永是国家领导人;if (str.Contains(胡长永)str = str.Replace( 胡长永, *); Console.WriteLine(str);Console.ReadKey();/20.查找指定文字的下标/查找天在“今天天气好晴朗,处处好风光!”句子中的下标 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string str = 今天天气好晴朗,处处好风光! ; int index = str.IndexOf(天);Console.WriteLine(index);Console.ReadKey();/21.去掉文本中的空格using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string s = hahah ;s = s.Trim();Console.Write(s);/s = s.TrimStart();/ 去掉文本的前半部分空格 /s = s.TrimEnd();/ 去掉文本的后半部分空格 Console.ReadKey();/22.判断字符串是否为空using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string str =null;/字符串为null/string str=null;/字符串不为空 /string str=;/字符串为空if (string.IsNullOrEmpty(str)Console.WriteLine(是的);elseConsole.WriteLine(不是);Console.ReadKey();/23.使用连接字符串数组using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string str = 张三, 李四, 王五 , 赵六, 刘二 ; string strnew = string.Join(|, str);Console.WriteLine(strnew);/string s = ;/for (int i = 0; i cba” using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)Console.WriteLine( 请输入一串字符:);/倒序反转法/string s = Console.ReadLine();/for (int i = s.Length - 1; i = 0; i-)/ Console.Write(si);/Console.ReadKey();char chs = s.ToCharArray();for (int i = 0; i “sharp c hello” using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)Console.WriteLine( 请输入一句英文:);string s = Console.ReadLine();char chs= ;string str = s.Split(chs, StringSplitOptions .RemoveEmptyEntries);for (int i = 0; i str.Length/2; i+)string temp = stri;stri = strstr.Length - 1 - i;strstr.Length - 1 - i = temp;s=string.Join( , str);Console.WriteLine(s);/与string.Join方法的效果一样/for (int i = 0; i str.Length; i+)/ Console.Write(stri+ );/ Console.ReadKey();/26.从email中提取出用户名和域名: abcusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)Console.WriteLine(请输入你的邮箱);string email = Console.ReadLine();/IndexOf用于提取指定字符在字符串中的位数int index = email.IndexOf();/Substring用于提出字符串中的子字符串从第 0位开始 string username = email.Substring(0, index); Console.WriteLine(用户名是:0, username); string domain = email.Substring(index + 1); Console.WriteLine(域名是:0, domain);Console.ReadKey();/27.文本文件中存储了多个文章标题、作者,/标题和作者之间用若干空格隔开,每行一个,/标题有的长有的短,输出到控制台的时候最多标题长度 10,/如果超过10,则截取长度 8的子串并且最后添加“.”,/加一个竖线后输出作者的名字using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)/指定读取文件的路径并按默认编码显示赋值给字符串数组/string contents = System.IO.File.ReadAllLines(C:Documents andSettingsAdministrator桌面1.txt, Encoding.Default);string path = C:Documents and SettingsAdministrator 桌面1.txt;string contents = File.ReadAllLines(path, Encoding.Default);for (int i = 0; i 10?strNew0.Substring(0,8)+ .:strNew0)+ | +strNew1);Console.ReadKey();/28.用户输入一行字母找出所有 E的位置using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)Console.WriteLine(请输入一行字母:);string s = Console.ReadLine();int index = s.IndexOf(e);int count=1;while(index!=-1)count+;index = s.IndexOf( e, index + 1);if(index=-1)break;Console.WriteLine(第0次出现e的位置是1, count, index);Console.ReadKey();/29.让用户输入一句话,查看有没有包含邪恶的词或字把它替换为 *如“邪恶”替换成“ *”using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string str = 老牛很邪恶;if(str.Contains(邪恶)str=str.Replace( 邪恶,*);Console.WriteLine(str);Console.ReadKey();/30.把诸葛亮,鸟叔 ,卡卡西,卡哇伊变成诸葛亮鸟叔卡卡西卡哇伊,然后再把切割掉 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;namespace 字符串练习class Programstatic void Main(string args)string names = 诸葛亮, 鸟叔, 卡卡西, 卡哇伊 ;string newnames = string.Join(|, names);Console.WriteLine(newnames);string str = newnam
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


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