测验成绩计算器1.现在有5门课程的成绩,在程序运行时,请随机给出它们的成绩;2.计算该学生成绩的平均值,并保留2位小数3.根据平均值输出相关信息(成绩分5个等级:A:>=90:B>=80且<90;C:>=70日<80:D:>=60<70:E:<60)c#
时间: 2024-09-20 20:03:02 浏览: 59
在C#中,你可以创建一个简单的控制台应用程序来实现这个测验成绩计算器。这里是一个简化的步骤:
1. 首先,导入必要的命名空间:
```csharp
using System;
using System.Collections.Generic;
```
2. 创建一个`Random`对象用于生成随机成绩:
```csharp
Random random = new Random();
```
3. 定义一个成绩类(StudentScore):
```csharp
public class StudentScore
{
public string Subject { get; set; }
public int Score { get; set; }
}
```
4. 主函数中获取并显示五门课程的成绩:
```csharp
static void Main()
{
List<StudentScore> scores = GenerateRandomScores(5);
Console.WriteLine("学生各科成绩:");
foreach (StudentScore score in scores)
{
Console.WriteLine($"{score.Subject}: {score.Score}");
}
}
```
5. 生成随机成绩列表:
```csharp
private static List<StudentScore> GenerateRandomScores(int count)
{
List<StudentScore> result = new List<StudentScore>();
for (int i = 0; i < count; i++)
{
int subjectIndex = i + 1;
string subject = $"课程{subjectIndex}";
int score = random.Next(60, 101); // 随机范围60到100
result.Add(new StudentScore { Subject = subject, Score = score });
}
return result;
}
```
6. 计算平均值并分类输出:
```csharp
double average = CalculateAverage(scores);
string grade = GetGrade(average);
Console.WriteLine($"平均分:{average:F2} 分");
Console.WriteLine($"评分为:{grade}");
}
private static double CalculateAverage(List<StudentScore> scores)
{
double sum = 0;
foreach (StudentScore score in scores)
{
sum += score.Score;
}
return sum / scores.Count;
}
private static string GetGrade(double average)
{
if (average >= 90) return "A";
else if (average >= 80) return "B";
else if (average >= 70) return "C";
else if (average >= 60) return "D";
else return "E"; // 如果小于60
}
```
阅读全文