C# 用类实现下面的功能通过界面输入多个学生信息名字成绩地址家庭联系方式学校 然后输出所有学生信息到界面上查询学生的信息输入学生姓名显示该学生所有相关信息提示步骤:创建一个学生类里面包含5个字段:名字成绩地址家庭联系方式学校包含一个方法:显示学生信息
using System;
using System.Collections.Generic;
namespace StudentManagement
{
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
// 输入学生信息
Console.WriteLine("输入学生信息(输入exit退出):");
while (true)
{
Console.Write("姓名:");
string name = Console.ReadLine();
if (name == "exit")
{
break;
}
Console.Write("成绩:");
int score = Convert.ToInt32(Console.ReadLine());
Console.Write("地址:");
string address = Console.ReadLine();
Console.Write("家庭联系方式:");
string contact = Console.ReadLine();
Console.Write("学校:");
string school = Console.ReadLine();
Student student = new Student(name, score, address, contact, school);
students.Add(student);
}
// 输出所有学生信息
Console.WriteLine("所有学生信息:");
foreach (Student student in students)
{
student.DisplayInfo();
}
// 查询学生信息
Console.Write("输入学生姓名进行查询:");
string queryName = Console.ReadLine();
Student foundStudent = students.Find(student => student.Name == queryName);
if (foundStudent != null)
{
Console.WriteLine("查询结果:");
foundStudent.DisplayInfo();
}
else
{
Console.WriteLine("找不到该学生信息。");
}
}
}
class Student
{
public string Name { get; set; }
public int Score { get; set; }
public string Address { get; set; }
public string Contact { get; set; }
public string School { get; set; }
public Student(string name, int score, string address, string contact, string school)
{
Name = name;
Score = score;
Address = address;
Contact = contact;
School = school;
}
public void DisplayInfo()
{
Console.WriteLine($"姓名:{Name}");
Console.WriteLine($"成绩:{Score}");
Console.WriteLine($"地址:{Address}");
Console.WriteLine($"家庭联系方式:{Contact}");
Console.WriteLine($"学校:{School}");
Console.WriteLine();
}
}
}
``
原文地址: https://www.cveoy.top/t/topic/hWDm 著作权归作者所有。请勿转载和采集!