C# 通过界面输入多个学生信息名字和分数输出所有输入的学生姓名和分数查询学生的分数输入学生姓名显示该学生的分数用函数实现逻辑可参考下面内容写函数然后通过主函数调用下面三个函数得到结果函数1输入所有学生姓名和分数函数2输出所有学生姓名和分数函数3查询学生分数通过输入学生名字输出学生分数
下面是完整的C#代码实现:
using System; using System.Collections.Generic;
namespace StudentManagement { class Program { static Dictionary<string, int> students = new Dictionary<string, int>();
static void Main(string[] args)
{
Console.WriteLine("请输入学生信息,格式为:姓名 分数");
InputStudents();
Console.WriteLine("所有学生信息如下:");
OutputStudents();
Console.WriteLine("请输入要查询分数的学生姓名:");
string name = Console.ReadLine();
QueryScore(name);
}
static void InputStudents()
{
while (true)
{
string input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) break;
string[] parts = input.Split(' ');
if (parts.Length != 2) continue;
string name = parts[0];
int score;
if (!int.TryParse(parts[1], out score)) continue;
students[name] = score;
}
}
static void OutputStudents()
{
foreach (var student in students)
{
Console.WriteLine("{0} {1}", student.Key, student.Value);
}
}
static void QueryScore(string name)
{
int score;
if (students.TryGetValue(name, out score))
{
Console.WriteLine("{0}的分数是:{1}", name, score);
}
else
{
Console.WriteLine("未找到该学生的信息");
}
}
}
原文地址: https://www.cveoy.top/t/topic/hsQO 著作权归作者所有。请勿转载和采集!