<C#任务导引教程>练习六

//54五名学生参加了两门课程的考试,试输入成绩,统计出各科的最高分,最低分,平均分及每个学生的平均成绩(要求用对话框显示成绩统计结果)
using System;
using System.Windows.Forms;
class Program
{
    static void Main()
    {
        const int N = 5;
        int[,] score = new int[N, 2];//score二维数组
        float[] ang = new float[N];//ang二维数组
        input(score);//输入函数
        avgAll(score, ang);//
        Console.WriteLine(" 课程1 课程2 平均分:");
        Console.WriteLine(showScore(score, ang));
        Console.WriteLine( );
        int x1, x2;
        minmum(score, out x1, out x2);
        Console.WriteLine(" 最低分:  " + x1 + " " + x2);
        maxmum(score, out x1, out x2);
        Console.WriteLine(" 最高分:  " + x1 + " " + x2);
        float a1, a2;
        average(score, out a1, out a2);
        Console.WriteLine(" 平均分:  " + a1 + " " + a2);
    }
    static void input(int[ , ] score)
    {
        string str;
        int n = 0;
        Console.WriteLine("请输入每个学生的各科成绩 ");
        for(int i=0;i<score.GetLength(0);i++)
        {
            for (int j = 0; j < score.GetLength(1); j++)
            {
                do
                {
                    Console.Write("学生{0}课程{1}成绩 ", i + 1, j + 1);
                    str = Console.ReadLine();
                    if (str == " ")
                        continue;
                    try
                    {
                        n = int.Parse(str);
                    }
                    catch//检查异常
                    {
                        Console.WriteLine("只能输入数字型数据");
                        Application.Exit();
                    }
                } while (n < 0 || n > 100);
                score[i,j]=n;
            }
                
        }
    }
static string showScore(int[ , ]score,float[ ]ang)//使用avgAll()函数
{
    string str = " ";
    for (int i = 0; i < score.GetLength(0); i++)
    {
        str += " 学生" + (i + 1) + ":    ";
        str += score[i, 0] + " " + score[i, 1] + " " + ang[i];
    }
    return str;
}
static void minmum(int[,] score, out int x1, out int x2)
{
    int an = score.GetLength(0);
    x1 = score[0, 0];
    x2 = score[0, 1];
    for (int i = 1; i < an; i++)
        if (score[i, 0] < x1)
            x1 = score[i, 0];
    for (int i = 1; i < an; i++)
    {
        if (score[i, 1] < x2)
            x2 = score[i, 1];
    }
}
static void maxmum(int[,] score, out int x1, out int x2)
{
    int an = score.GetLength(0);
    x1 = score[0, 0];
    x2 = score[0, 1];
    for (int i = 1; i < an; i++)
        if (score[i, 0] > x1)
            x1 = score[i, 0];
    for (int i = 1; i < an; i++)
        if (score[i, 1] > x2)
            x2 = score[i, 1];
}
static void average(int[,] score, out float a1, out float a2)
{
    int sum1 = 0, sum2 = 0;
    int an = score.GetLength(0);
    for (int i = 0; i < an; i++)
        sum1 += score[i, 0];
    for (int i = 0; i < an; i++)
        sum2 += score[i, 1];
    a1 = sum1 * 1.0f / an;
    a2 = sum2 * 1.0f / an;
}
static void avgAll(int[,] score, float[] ang)//avgAll()函数
{
    int nj = score.GetLength(1);
    for(int i=0;i<score.GetLength(0);i++)
    {
        int sum = 0;
        for (int j = 0; j < nj; j++)
            sum += score[i, j];
        ang[i] = sum * 1.0f / nj;
    }
}
}

原文地址:https://www.cnblogs.com/zhangyongjian/p/3623000.html