C#基础实例.docx

上传人:b****1 文档编号:2440490 上传时间:2022-10-29 格式:DOCX 页数:98 大小:42.40KB
下载 相关 举报
C#基础实例.docx_第1页
第1页 / 共98页
C#基础实例.docx_第2页
第2页 / 共98页
C#基础实例.docx_第3页
第3页 / 共98页
C#基础实例.docx_第4页
第4页 / 共98页
C#基础实例.docx_第5页
第5页 / 共98页
点击查看更多>>
下载资源
资源描述

C#基础实例.docx

《C#基础实例.docx》由会员分享,可在线阅读,更多相关《C#基础实例.docx(98页珍藏版)》请在冰豆网上搜索。

C#基础实例.docx

C#基础实例

基础知识复习练习题

1.纺写一段程序,运行时向用提问“你考了多少分?

(0-100)”接受输入后判断其等级并显示出来。

判断依据如下:

等则={优(90~100);良(80~89);中(70~79);下{60~69};差(0~59)}

2.编程输出九九乘法表。

3.定义长度50的数组随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组int[]array=newint[50];Randomr=newRandom();r.Next();(注:

这道题有点问题如果用户输入的数大于10将无法按按用户输入的数进行输出一行多少个)

4.编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命名规范骆驼命名)names.SubString(0,1)s.SubString

(1)

5.编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成大小然后返回(命名规范帕斯卡)

6.声明两个变量:

intn1=10,n2=20;要求将两个变量交换,最后输出n1为20,n2为10.扩展(*):

不使用桃花汛三个变量如何交换?

7.用方法来实现:

将上题封装一个方法来做。

提示:

方法有两个参数n1,n2,在方法中将n1,n2进行交换使用ref

8.请用户输入一个字符串,计算字符串中的字符个数,并输出。

9.用方法来实现:

计算两个数的最大值,思考:

方法的参数?

返回值?

扩展:

计算任意多个数间的最大值(提示:

params)

10.用方法来实现:

计算1-100之间的所有整数的和。

11.用方法来实现:

计算1-100之间的所有奇数的和。

12.用方法来实现:

判断一个给定的整数是否为“质数”。

13.用方法来实现:

计算1-100之间的所有质数(素数)的和。

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个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。

18.有如下字符串:

“患者:

大夫,我咳嗽得很重”“大夫,你多大年纪”患者:

七十五岁,大夫:

那现在不咳嗽,还要等到什么时咳嗽?

需求:

请统计出该字符中,咳嗽二字的出现次数,以及每次咳嗽出现的索引位置。

 

//1.用方法来实现:

有一个字符串数组:

//{"马龙","迈克尔乔丹","雷吉米勒","蒂姆邓肯","科比布莱恩特"},请输出最长的

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace方法练习

{

classProgram

{

staticvoidMain(string[]args)

{

string[]names={"马龙","迈克尔乔丹","雷吉米勒","蒂姆邓肯","科比布莱恩特"};

stringmax=GetLongest(names);

Console.WriteLine(max);

Console.ReadKey();

}

publicstaticstringGetLongest(string[]s)

{

stringmax=s[0];

for(inti=0;i

{

if(s[i].Length>max.Length)

{max=s[i];}

}

returnmax;

}

}

}

//2.写一个方法,用来判断用户输入的数字是不是质数

//再写一个方法,要求用户只能输入数字输入有误就一直让用户输入

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace方法练习

{

classProgram

{

staticvoidMain(string[]args)

{

while(true)

{

Console.WriteLine("请输入一个数字,我们将判断你输入的数字是否为质数");

stringstrNumber=Console.ReadLine();

intnumber=GetNumber(strNumber);

boolb=IsPrime(number);

Console.WriteLine(b);

Console.ReadKey();

}

}

publicstaticboolIsPrime(intnumber)

{

if(number<2)

{returnfalse;}

else

{

for(inti=2;i

{

if(number%i==0)

{

returnfalse;

}

}

returntrue;

}

}

publicstaticintGetNumber(stringstrNumber)

{

while(true)

{

try

{

intnumber=Convert.ToInt32(strNumber);

returnnumber;

}

catch

{

Console.WriteLine("请重新输入");

strNumber=Console.ReadLine();

}

}

}

}

}

//3.接收输入后判断其等级并显示出来

//判断依据如下:

等级={优(90,100分);良(80~89分)

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace方法练习1

{

classProgram

{

staticvoidMain(string[]args)

{

Console.WriteLine("请输入考试成绩");

intscore=Convert.ToInt32(Console.ReadLine());

stringlevel=GetLevel(score);

Console.WriteLine(level);

Console.ReadKey();

}

publicstaticstringGetLevel(intscore)

{

stringlevel="";

switch(score/10)

{

case10:

case9:

level="优";

break;

case8:

level="良";

break;

case7:

level="中";

break;

case6:

level="差";

break;

case5:

level="不及格";

break;

}

returnlevel;

}

}

}

另外一种方法

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace基础练习01

{

classProgram

{

staticvoidMain(string[]args)

{

Console.WriteLine("请输入考试成绩");

intsum=Convert.ToInt32(Console.ReadLine());

stringlevel=GetLevel(sum);

Console.WriteLine(level);

Console.ReadKey();

}

publicstaticstringGetLevel(intsum)

{

stringlevel=null;

if(sum>=90)

{

level="优";

}

elseif(sum>=80)

{

level="良";

}

elseif(sum>=70)

{

level="中";

}

elseif(sum>=60)

{

returnlevel="差";

}

elseif(sum<60)

{

level="不及格";

}

returnlevel;

}

}

}

 

//4.请将字符串数组{“中国”,“美国”,“巴西”,“澳大利亚”,“加拿大”}中的元素反转输出

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespace方法练习1

{

classProgram

{

staticvoidMain(string[]args)

{

string[]names={"中国","美国","巴西","澳大利亚","加拿大"};

Test(names);

for(inti=0;i

{

Console.Write(names[i]+",");

}

Console.ReadKey();

}

publicstaticvoidTest(string[]names)

{

for(inti=0;i

{

stringtemp=names[i];

names[i]=names[names.Length-1-i];

names[names.Length-i-1]=temp;

}

}

}

}

//5.循环录入5个人的年龄并计算平均年龄,

//如果录入的数据出现负数或大于100的数,立即停止输入并报错。

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespace练习2

{

classProgram

{

staticvoidMain(string[]args)

{

intsum=0;

boolb=true;

for(inti=0;i<5

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1