【发布时间】:2021-01-15 11:24:49
【问题描述】:
所以我的程序中有一个对象列表,其中的对象具有(姓名、姓氏、id、房子等) 我想创建一个名为 search 的方法,它可以让我仅通过其名称查找特定对象。我想在搜索后打印出信息。 (我有一个家长班被称为向导和两个孩子班。一个给老师,一个给学生(WizardToBe)。)
List<WizardTobe> WizardStudents = new List<WizardTobe>();
我使用 Readline 收到了以下所需的所有信息,因此所有这些都是用户输入的。 我所有的代码都在一个while循环中。
string studentname = Console.ReadLine();
Console.WriteLine("Your last name?");
string studentlast = Console.ReadLine();
Console.WriteLine("And you my child, which house are you from?");
string studentHouse = Console.ReadLine();
Console.WriteLine("And may i know your wizard ID?");
string studentWizID = Console.ReadLine();
Console.WriteLine("And what creature is your lovely Familiar?");
string studentFamiliar = Console.ReadLine();
Console.WriteLine("What is your student ID?");
string babywizID = Console.ReadLine();
Console.WriteLine("sorry this is taking long. Lastly! in School year, what year are you curently in?");
string yearschool = Console.ReadLine();
WizardStudents.Add(new WizardTobe(studentname, studentlast, studentWizID, studentHouse, studentFamiliar, babywizID, yearschool));
我尝试将我想添加到列表中的对象命名为“学生名”,这是我通过 Readline 收到的。但我遇到了另一个错误...... 我该怎么办 ? :(
我的课程是: 我的父类
using System;
using System.Collections.Generic;
using System.Text;
namespace HogwartsSignUp
{
public class Wizard
{
string name;
string lastname;
string wizID;
string house;
//teacher
private string yearclass;
private string workID;
//students
private string familiar;
private string studentID;
private string studentyear;
public Wizard(string name, string lastname, string wizID, string house, string familiar, string studentID, string studentyear)
{
this.name = name;
this.lastname = lastname;
this.wizID = wizID;
this.house = house;
this.studentID = studentID;
this.familiar = familiar;
this.studentyear = studentyear;
}
public Wizard(string name, string lastname, string wizID, string house, string yearclass, string workID)
{
this.name = name;
this.lastname = lastname;
this.wizID = wizID;
this.house = house;
this.workID = workID;
this.yearclass = yearclass;
}
public void showStudentInfo()
{
Console.WriteLine("They are a Hogwarts student! Here's the information of them:");
Console.WriteLine("Name: {0} Last name: {1} House: {2} Year: {3} Familiar: {4}", name, lastname,house,studentyear,familiar);
}
public void showTeacherInfo()
{
Console.WriteLine("They are a Hogwarts teacher! Here's the information of them:");
Console.WriteLine("Name:{0} Last Name:{1} House:{2} Teacher of the class: {3}", name, lastname, house, yearclass);
}
}
}
我的孩子班:
using System;
using System.Collections.Generic;
using System.Text;
namespace HogwartsSignUp
{
public class WizardTobe : Wizard
{
string magicwand;
string sportinterest;
string hobby;
public WizardTobe(string name, string lastname, string wizID, string house, string familiar, string studentID, string studentyear) : base (name, lastname, wizID, house, familiar, studentID, studentyear)
{
}
}
}
【问题讨论】:
-
WizardStudents.Where(s => s.name == somename);可能有用,不知道你还没有发布这些课程。 -
是的,我想找一个有名字的学生。
-
我想在我的列表中搜索,只有用户输入他们的名字
标签: c# list inheritance search console