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];
            }
            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);

            for (int i = 0; i < 4; i++)
            {
    
    
                for (int j = 0; j < 5; j++)
                    Console.Write("{0}\t", arr[i, j]);
                Console.WriteLine();
            }

            for (int i = 0; i < 4; i++)
            {
    
    
                aver2[i] = 0;
                for (int j = 0; j < 5; j++)
                {
    
    
                    aver2[i] += arr[i, j];
                }
                aver2[i] /= 5 * 1.0;
            }
            for (int i = 0; i < 4; i++)
            {
    
    
                Console.WriteLine("第" + (i + 1) + "行的平均值为: {0}", aver2[i]);
            }
            Console.WriteLine();

            for (int i = 0; i < 5; i++)
            {
    
    
                 aver1[i] = 0;
                 for (int j = 0; j < 4; j++)
                 {
    
    
                     aver1[i] += arr[j, i];
                 }
                 aver1[i] /= 4 * 1.0;
            }
            for (int i = 0; i < 5; i++)
            {
    
    
                Console.WriteLine("第" + (i + 1) + "列的平均值为: {0}", aver1[i]);
            }
            Console.ReadLine();
        }
    }
}

实验的运行效果如下:

在这里插入图片描述
在这里插入图片描述

四、实验总结

  • 1、通过本次实验总结掌握了数组的输入与输出的方法。
  • 2、掌握了C#数组的相关的程序算法。
  • 3、掌握了数组的Length属性可以获取数组的长度等。
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_47419053/article/details/127140360