语言:C#。题目:3.键盘接收n位同学姓名和成绩,输出分数最高的同学的姓名和分数,输出他们的平均分和高于平均分的同学姓名。要求:用多维数组
时间: 2024-09-30 20:09:58 浏览: 52
在C#中,要实现这个需求,你可以使用二维数组或自定义学生类结合List<T>来存储数据,并通过循环遍历比较找出最高分、计算平均分以及筛选出高于平均分的学生。这里是一个简化的示例:
```csharp
using System;
using System.Collections.Generic;
class Program
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
static void Main(string[] args)
{
Console.Write("请输入学生数量(n): ");
int n = Convert.ToInt32(Console.ReadLine());
List<Student> students = new List<Student>();
for (int i = 0; i < n; i++)
{
Console.WriteLine($"请输入第{i + 1}名学生的姓名: ");
string name = Console.ReadLine();
Console.WriteLine($"请输入第{i + 1}名学生的分数: ");
int score = Convert.ToInt32(Console.ReadLine());
Student student = new Student { Name = name, Score = score };
students.Add(student);
}
// 找到最高分学生
Student maxScoreStudent = students[0];
foreach (var student in students)
{
if (student.Score > maxScoreStudent.Score)
{
maxScoreStudent = student;
}
}
Console.WriteLine($"最高分学生是{maxScoreStudent.Name},分数是{maxScoreStudent.Score}");
// 计算平均分
double totalScore = students.Sum(s => s.Score);
double averageScore = totalScore / n;
Console.WriteLine($"平均分为{averageScore}");
// 筛选出高于平均分的学生
List<Student> aboveAverageStudents = students.Where(s => s.Score > averageScore).ToList();
Console.WriteLine("高于平均分的学生有:");
foreach (var student in aboveAverageStudents)
{
Console.WriteLine($"{student.Name}, 分数:{student.Score}");
}
阅读全文