C#面向对象程序设计课程实验三:实验名称:C#数组和集合
实验内容:C#数组和集合
一、实验目的
实验目的及要求
- (1)掌握数组声明与创建;
- (2)掌握数组的引用及初始化;
- (3) 利用调试程序来修改程序的逻辑错误;
二、实验环境
Microsoft Visual Studio 2008
三、实验内容与步骤
3.1.1、实验内容
- 定义一个数组,用来存储输入的 10 个学生的考试成绩,计算并输出平均成绩、 最高成绩和最低成绩及其对应的数组下标。
- 项目名称为 XT5-2,程序的运行界面如 图所示。(教材第 5 章 140 页 3.2 题)
注意考虑以下情况:
①输入成绩在 0-100 的范围之外的处理。
②如果输入的成绩后面带有分号等标点符号,也可以过滤。
③当有多个相同的最高或最低分时,也能分别显示出来。
3.1.2、实验步骤
实验的程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 实验三_1_
{
class Program
{
static double Average(int[] Scores)
{
double total = 0;
for (int i = 0; i < Scores.GetLength(0); i++)
total += Scores[i];
return (double)(total / Scores.GetLength(0));
}
static void Main(string[] args)
{
int[] Scores = new int[10];
for (int i = 0; i < Scores.Length; i++)
{
int a;
Console.Write("请输入第{0}个学生成绩 :", i);
a = int.Parse(Console.ReadLine());
Scores[i] = a;
}
int a1 = 0;
int high = 0;
for (int i = 0; i < 10; i++)
{
if (Scores[i] > high)
{
high = Scores[i];
a1 = i;
}
}
int a2 = 0;
int low =100;
for (int j = 0; j < 10; j++)
{
if (Scores[j] < low)
{
low = Scores[j];
a2 = j;
}
}
Console.WriteLine("平均成绩: {0}", Average(Scores));
Console.WriteLine("最高成绩: {0},下标是: {1}",high ,a1);
Console.WriteLine("最低成绩: {0},下标是: {1}",low ,a2);
Console.ReadLine();
}
}
}
注意考虑以下情况:
- ①输入成绩在 0-100 的范围之外的处理。
可以将这个语句a = int.Parse(Console.ReadLine());
的后面添加语句为
if (a < 0 || a > 100)
{
Console.WriteLine("输入错误");
a = a - 1;
}
else
Scores[i] = a;
- ②如果输入的成绩后面带有分号等标点符号,也可以过滤。
a = Console.ReadLine();
a = Regex.Replace(a,@"[^\d]\*", "");
使用Regex.Replace将其标点转换成字符。
在用int.Parse转换为整形数字。
- ③当有多个相同的最高或最低分时,也能分别显示出来。
int high = 0;
for (int i = 0; i < 10; i++)
{
If (Scores[i] > high)
high = Scores[i];
![img](https://img-blog.csdnimg.cn/img_convert/23f8886da431a546d2488c12061d021d.png)
![img](https://img-blog.csdnimg.cn/img_convert/e6abf7a29d6c400b9cacd9e361afdb4b.png)
![img](https://img-blog.csdnimg.cn/img_convert/8a5ee554d636ff8d669c6df996ff700d.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**