《2022年字符串数组练习题答案参照 .pdf》由会员分享,可在线阅读,更多相关《2022年字符串数组练习题答案参照 .pdf(7页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、1 字符串数组练习答案1.用选择排序法对10个整数从小到大进行排序。要求:在一个类中用 Main()来实现排序算法。using System;namespace 选择排序class Class1staticvoid Main()int i,j,k,temp;int a=new int 918,124,58,1125,825,51,82,26,99,68;for(i=0;ia.Length-1;i+)k=i;for(j=i+1;jaj)k=j;if(k!=i)temp=ai;ai=ak;ak=temp;Console.WriteLine(Result of sort is:);for(i=0;i
2、a.Length;i+)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -2 Console.WriteLine(0,ai);Console.ReadLine();运行结果:2.计算长度为 10 的一维整数数组各偶数项元素值之和。using System;namespace 偶数项元素值之和class Class1staticvoid Main()int a,b=0;int c=new int 1,2,3,4,5,6,7,8,9,10;for(a=0;ac.Length;a+)if(a%2=0)b+=ca;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7
3、页 -3 Console.WriteLine(Sun of the even is:0,b);运行结果:3.声明一个长度为6 的一维数组作为一个类的成员,在构造方法中实例化和填充数组每个数组元素的值是其下标值的平方,然后用一个方法打印该数组的各元素的值。using System;namespace 用构造方法实例化和填充数组class ArryApp int numbers;public ArryApp()numbers=new int 6;for(int i=0;i6;i+)numbersi=i*i;publicvoid PrinArry()for(int i=0;inumbers.Leng
4、th;i+)Console.WriteLine(numbers0=1,i,numbersi);名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -4 staticvoid Main()ArryApp app=new ArryApp();app.PrinArry();运行结果:4.输入数组各元素,求其和。using System;namespace 输入数组各元素求其和class Class1 staticvoid Main()Console.WriteLine(Enter size of array:);string s=Console.ReadLine();int Siz
5、e=int.Parse(s);long MyArray=new long Size;long Sum=0;名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 7 页 -5 for(int i=0;iMyArray.Length;i+)Console.Write(Enter element of MyArray0:,i);s=Console.ReadLine();MyArrayi=int.Parse(s);for(int i=0;iMyArray.Length;i+)Sum+=MyArrayi;Console.WriteLine(Sum=0,Sum);运行结果:5.假设某班 10 人,
6、每名同学一次参加5 门课考试,要求计算每名同学的总分和每门课的全班平均分。using System;namespace 学生总分和各科平均分名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 7 页 -6 class Class1staticvoid Main()constint Pupil=10;/定义整型常量 Pupil 为10个学生const int Class=5;/定义整型常量 Class为5门课程int,Score=78,90,89,85,92,89,85,79,100,95,60,70,80,90,95,95,85,75,65,60,94,86,78,89,95,78,
7、90,89,85,92,89,85,79,100,95,60,70,80,90,95,95,85,75,65,60,94,86,78,89,95;int i,j;/循环控制变量int Sum;/存储总的分变量int Aver=0,0,0,0,0;/存储各门平均成绩的一维数组Console.WriteLine(学号总分);for(i=0;iPupil;i+)Sum=0;for(j=0;jClass;j+)Sum=Sum+Scorei,j;/求出第 i 个学生 5门课总成绩 Averj=Averj+Scorei,j;/每门课 50个学生的成绩和 Console.WriteLine(No.0 1,i+1,Sum);Console.WriteLine(科目平均成绩是:);for(j=0;jClass;j+)Averj/=Pupil;/每门课的平均成绩名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 7 页 -7 Console.WriteLine(0 ,Averj);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 7 页 -