一、实验目的
实验目的及要求
- (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];
}
Console.Write("最高成绩为{0},下标是", high);
for (i = 0; i < 10; i++)
{
if (a[i] == high)
Console.Write(",{0}", high +1);
}
- 这样可以在有多个相同的最高分时,也能分别显示出来。同理,最低分也用同样思路。
实验的运行效果如下:
3.2.1、实验内容
- 定义一个 4X5 的二维数组,使元素值为行、列号之积,然后输出此矩阵并计算每一列的平均值。
- 项目名称为 xt5-4,程序的运行界面如图所示。(教材第 5 章 140 页 3.4 题)。
3.2.2、实验步骤
实验的程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 实验三_2_
{
class Program
{
static void Main(string[] args)
{
double[] aver1 = new double[5];
double [] aver2=new double [4];
int[,] arr = new int[4, 5];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
arr[i, j] = (i + 1) \* (j + 1);



**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**