C#编写代码实现接收一个班级五名学员的姓名和三门课程的考试成绩,计算每 名学员三门课程的总成绩、平均成绩。并输出学员姓名、三门课的成绩、总成绩和平 均成绩,以及班级参加考试人数,三门课总和的最高分和平均分
时间: 2024-02-05 10:10:49 浏览: 95
C语言程序设计-输入2个学生3门课的成绩,计算学生成绩平均分,课程平均分和最高分.
5星 · 资源好评率100%
下面是一份C#代码实现:
```csharp
using System;
namespace ScoreCalculator
{
class Program
{
static void Main(string[] args)
{
const int NUM_OF_STUDENTS = 5;
const int NUM_OF_COURSES = 3;
string[] names = new string[NUM_OF_STUDENTS];
double[,] scores = new double[NUM_OF_STUDENTS, NUM_OF_COURSES];
double[] totals = new double[NUM_OF_STUDENTS];
double[] averages = new double[NUM_OF_STUDENTS];
double[] courseTotals = new double[NUM_OF_COURSES];
int numOfParticipates = 0;
double highestScore = 0;
double courseTotalSum = 0;
// Input student names and scores
for (int i = 0; i < NUM_OF_STUDENTS; i++)
{
Console.WriteLine($"Enter the name of student {i + 1}:");
names[i] = Console.ReadLine();
Console.WriteLine($"Enter the scores of student {i + 1}:");
for (int j = 0; j < NUM_OF_COURSES; j++)
{
Console.Write($"Course {j + 1}: ");
scores[i, j] = double.Parse(Console.ReadLine());
}
Console.WriteLine();
}
// Calculate totals and averages
for (int i = 0; i < NUM_OF_STUDENTS; i++)
{
double total = 0;
for (int j = 0; j < NUM_OF_COURSES; j++)
{
total += scores[i, j];
courseTotals[j] += scores[i, j];
courseTotalSum += scores[i, j];
}
totals[i] = total;
averages[i] = total / NUM_OF_COURSES;
if (totals[i] > highestScore)
{
highestScore = totals[i];
}
if (totals[i] > 0)
{
numOfParticipates++;
}
}
// Output results
Console.WriteLine("Name\tCourse 1\tCourse 2\tCourse 3\tTotal\tAverage");
for (int i = 0; i < NUM_OF_STUDENTS; i++)
{
Console.Write($"{names[i]}\t");
for (int j = 0; j < NUM_OF_COURSES; j++)
{
Console.Write($"{scores[i, j]}\t\t");
}
Console.Write($"{totals[i]}\t{averages[i]}\n");
}
Console.WriteLine($"Number of participates: {numOfParticipates}");
Console.WriteLine($"Highest score: {highestScore}");
Console.WriteLine($"Average score: {courseTotalSum / (NUM_OF_STUDENTS * NUM_OF_COURSES)}");
}
}
}
```
这份代码使用了一个二维数组 `scores` 来存储每个学生的成绩,一个一维数组 `totals` 来存储每个学生的总成绩,一个一维数组 `averages` 来存储每个学生的平均成绩,以及一个一维数组 `courseTotals` 来存储每门课程的总分数。在输入学生姓名和成绩后,代码遍历每个学生的成绩并计算总成绩、平均成绩以及每门课程的总分数。最后输出每个学生的姓名、成绩、总成绩和平均成绩,以及班级参加考试人数、三门课的总和的最高分和平均分。
阅读全文