using System;
namespace Console6
{
internal class Test3
{
static void Main(string[] args)
{
/*
.1、设计一个控制台应用程序,实现学生成绩的录入、统计与展示功能,具体要求如下:
. (1)通过控制台输入班级学生人数(人数需大于 0,若输入非法值需提示 “人数必须为正整数” 并重新输入);
. (2)定义一个double类型数组,存储该班级所有学生的数学成绩(成绩范围 0-100,若输入非法值需提示 “成绩必须在 0-100 之间” 并重新输入该学生成绩);
. (3)使用循环结构计算成绩的平均分(保留 1 位小数)、最高分、最低分,并统计及格人数(成绩≥60)与优秀人数(成绩≥90);
. (4)遍历数组,在控制台按 “学号(从 1 开始):成绩” 格式输出所有学生成绩,最后输出统计结果(格式参考 “平均分:85.5,最高分:98.0,最低分:52.0,及格人数:30,优秀人数:8”)。
*/
int n = 0;
// 循环直到输入有效的人数
while (n <= 0)
{
try
{
Console.WriteLine("请输入班级学生人数:");
n = Convert.ToInt32(Console.ReadLine());
if (n <= 0)
{
Console.WriteLine("人数必须为正整数");
}
}
catch (FormatException)
{
Console.WriteLine("请输入有效的整数");
}
catch (OverflowException)
{
Console.WriteLine("输入的数字超出范围");
}
}
double[] scores = new double[n];
// 录入学生成绩
for (int i = 0; i < n; i++)
{
bool validScore = false;
while (!validScore)
{
try
{
Console.WriteLine("请输入第" + (i + 1) + "个学生的成绩:");
double score = Convert.ToDouble(Console.ReadLine());
if (score < 0 || score > 100)
{
Console.WriteLine("成绩必须在 0-100 之间");
}
else
{
scores[i] = score;
validScore = true;
}
}
catch (FormatException)
{
Console.WriteLine("请输入有效的数字");
}
catch (OverflowException)
{
Console.WriteLine("输入的数字超出范围");
}
}
}
// 输出所有学生成绩
for (int i = 0; i < n; i++)
{
Console.WriteLine($"学号{i + 1}:{scores[i]:F1}");
}
// 计算统计数据
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += scores[i];
}
double avg = sum / n;
double max = scores[0];
double min = scores[0];
for (int i = 1; i < n; i++)
{
if (scores[i] > max)
{
max = scores[i];
}
else if (scores[i] < min)
{
min = scores[i];
}
}
int passCount = 0;
for (int i = 0; i < n; i++)
{
if (scores[i] >= 60)
{
passCount++;
}
}
int excellentCount = 0;
for (int i = 0; i < n; i++)
{
if (scores[i] >= 90)
{
excellentCount++;
}
}
// 输出统计结果
Console.WriteLine("平均分:" + avg.ToString("F1") + ",最高分:" + max.ToString("F1") + ",最低分:" + min.ToString("F1") + ",及格人数:" + passCount + ",优秀人数:" + excellentCount);
}
}
}
C#第二次上机作业
于 2025-10-09 16:41:39 首次发布
1030

被折叠的 条评论
为什么被折叠?



