C#随机产生一个班五名同学3门课程成绩,统计出每一门课程的最高分、最低分及平均分;
参考一下:
using System;
namespace ClassScore
{
class Program
{
static void Main(string[] args)
{
const int numStudents = 5; // 学生人数
const int numCourses = 3; // 课程数
// 随机生成成绩
Random rand = new Random();
int[,] scores = new int[numStudents, numCourses];
for (int i = 0; i < numStudents; i++)
{
for (int j = 0; j < numCourses; j++)
{
scores[i, j] = rand.Next(0, 101); // 0-100之间的整数
}
}
// 统计分数
int[] maxScores = new int[numCourses];
int[] minScores = new int[numCourses];
double[] avgScores = new double[numCourses];
for (int j = 0; j < numCourses; j++)
{
int maxScore = scores[0, j];
int minScore = scores[0, j];
double sumScore = 0;
for (int i = 0; i < numStudents; i++)
{
int score = scores[i, j];
if (score > maxScore)
{
maxScore = score;
}
if (score < minScore)
{
minScore = score;
}
sumScore += score;
}
maxScores[j] = maxScore;
minScores[j] = minScore;
avgScores[j] = sumScore / numStudents;
}
// 输出结果
Console.WriteLine("每门课程的最高分、最低分及平均分:");
for (int j = 0; j < numCourses; j++)
{
Console.WriteLine("第{0}门课程:最高分{1},最低分{2},平均分{3:F2}", j + 1, maxScores[j], minScores[j], avgScores[j]);
}
Console.ReadLine(); // 等待用户按回车键退出程序
}
}
}
//二维数组来存储班级成绩,第一维表示学生,第二维表示课程。随机生成成绩后,依次计算每门课程的最高分、最低分和平均分,并分别存储在三个数组中。最后输出结果。
可以使用C语言的随机数生成函数和数组来模拟班级五名同学三门课程成绩的情况,可以采用以下代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int score[5][3]; // 存储五名同学三门课的成绩
double avg[3] = {0}; // 存储每门课的平均分
int max[3] = {-1, -1, -1}; // 存储每门课的最高分,初始化为无穷小
int min[3] = {101, 101, 101}; // 存储每门课的最低分,初始化为无穷大
srand((unsigned int)time(NULL)); // 设置随机数种子
// 随机生成五名同学三门课的成绩
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
score[i][j] = rand() % 101; // 随机生成0到100的整数
printf("第%d名同学第%d门课成绩为:%d分\n", i + 1, j + 1, score[i][j]);
// 更新每门课的最高分、最低分以及累计总分
if (score[i][j] > max[j]) max[j] = score[i][j];
if (score[i][j] < min[j]) min[j] = score[i][j];
avg[j] += score[i][j];
}
}
// 计算每门课的平均分
for (int j = 0; j < 3; j++) {
avg[j] /= 5;
printf("第%d门课的最高分为:%d分,最低分为:%d分,平均分为:%.1lf分\n", j + 1, max[j], min[j], avg[j]);
}
return 0;
}
具体实现过程是随机生成五名同学三门课程的成绩,并统计每门课的最高分、最低分和平均分。在代码中,srand函数用于设置随机数种子,rand函数用于生成随机整数,score数组用于存储成绩,avg数组用于存储每门课的平均分,max和min数组分别用于存储每门课的最高分和最低分。
我可以为您提供以下思路:
首先,您需要使用C#中的随机数生成器来生成5个学生的3门课程成绩。您可以使用Random类来实现这个功能。
生成完成绩后,您需要计算每门课程的最高分、最低分和平均分。可以使用for循环遍历所有学生的成绩,找到最高分和最低分,并累加总分数以计算平均分数。
最后,您可以输出每门课程的最高分、最低分和平均分,以便查看结果。
以下是一个示例代码,可供参考:
using System;
class Program
{
static void Main(string[] args)
{
// 随机生成学生的成绩
Random random = new Random();
int[,] scores = new int[5, 3];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
scores[i, j] = random.Next(0, 101);
}
}
// 计算每门课程的最高分、最低分和平均分
int[] maxScores = new int[3];
int[] minScores = new int[3];
float[] avgScores = new float[3];
for (int i = 0; i < 3; i++)
{
int maxScore = int.MinValue;
int minScore = int.MaxValue;
int totalScore = 0;
for (int j = 0; j < 5; j++)
{
int score = scores[j, i];
totalScore += score;
if (score > maxScore)
{
maxScore = score;
}
if (score < minScore)
{
minScore = score;
}
}
maxScores[i] = maxScore;
minScores[i] = minScore;
avgScores[i] = totalScore / 5.0f;
}
// 输出结果
Console.WriteLine("第一门课程:最高分={0},最低分={1},平均分={2}", maxScores[0], minScores[0], avgScores[0]);
Console.WriteLine("第二门课程:最高分={0},最低分={1},平均分={2}", maxScores[1], minScores[1], avgScores[1]);
Console.WriteLine("第三门课程:最高分={0},最低分={1},平均分={2}", maxScores[2], minScores[2], avgScores[2]);
}
}