C#面向对象程序设计课程实验三:实验名称:C#数组和集合_c#计算平均成绩和标准差,将其输出。

34 阅读3分钟

一、实验目的

实验目的及要求

  • (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);


![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/29d160d98db84c8fb713604ced7bc87b~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771866851&x-signature=FcfXvirV2fEIlOUcqJUvL0guiDQ%3D)
![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/03e26fedcd8b4924a6b3e98c0ce4586f~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771866851&x-signature=DN%2FQlP9%2BuBb6xWuklKqSdYWj7%2BM%3D)
![img](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/2de79a45582246eeb7ca9a9ee716a58e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1771866851&x-signature=H7YnuYvGq84fuOXbcB1z%2BFaPIRs%3D)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**


**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**