如何遍历对象列表并在C#中搜索字符串

时间:2016-01-10 13:58:41

标签: c# list oop for-loop foreach

我创建了一个应用程序,您可以将学生和讲师添加到每个列表中,我需要搜索列表以按名称或ID查找学生或讲师。但是,当我循环遍历列表以查找匹配的字符串时,它将找到匹配的字符串并打印详细信息,但是对于列表中的每个其他成员,将触发说明未找到学生的else语句。我尝试在if语句后暂停,但它仍然发生。我该如何解决这个问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBSManagement
{
    class College: Staff
    {
        public static List<Student> students = new List<Student>();
        public static List<Lecturer> lecturers = new List<Lecturer>();

        static void Main()
        { 
            int choice;
            bool seeAgain = true;

                do
                {
                    Console.WriteLine("Press");
                    Console.WriteLine("1: To add a student");
                    Console.WriteLine("2: To add a lecturer");
                    Console.WriteLine("3: To search for a lecturer or student");
                    Console.WriteLine("4: To show the details of all enrolled students");
                    Console.WriteLine("5: To show the names of all lecturers");
                    Console.WriteLine("6: To show payroll details for a lecturer");
                    Console.WriteLine("7: To quit");
                    int.TryParse(Console.ReadLine(), out choice);

                    switch (choice)
                    {
                        case 1:
                            AddStudent();
                            break;
                        case 2:
                            AddLecturer();
                            break;
                        case 3:
                            SearchPerson();
                            break;
                        case 4:
                            ShowStudents();
                            break;
                        case 5:
                            ShowLecturers();
                            break;
                        case 6:
                            ShowPayrollDetails();
                            break;
                        case 7:
                            seeAgain = false;
                            break;
                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                } while (seeAgain);
            }
        public static void AddStudent()
        {
            Student student = new Student();
            Console.WriteLine("Enter student name:");
            student.Name = Console.ReadLine();
            Console.WriteLine("Enter student address:");
            student.Address = Console.ReadLine();
            Console.WriteLine("Enter student phone number:");
            student.Phone = Console.ReadLine();
            Console.WriteLine("Enter student email:");
            student.Email = Console.ReadLine();
            Console.WriteLine("Enter student PPSN:");
            student.PPSN = Console.ReadLine();
            Console.WriteLine("Enter student status (postgrad or undergrad):");
            EnterStat:
                string stat = Console.ReadLine().ToLower();
                if (stat == "postgrad" || stat == "undergrad")
                {
                    student.Status = (Status)Enum.Parse(typeof(Status), stat);
                }
                else
                {
                    Console.WriteLine("Please enter either postgrad or undergrad:");
                goto EnterStat;
                }
            Console.WriteLine("Enter student ID:");
            int inStudentID;
            int.TryParse(Console.ReadLine(), out inStudentID);
            student.StudentID = inStudentID;
            students.Add(student);
        }

        public static void AddLecturer()
        {
            Lecturer lecturer = new Lecturer();
            Console.WriteLine("Enter lecturer name:");
            lecturer.Name = Console.ReadLine();
            Console.WriteLine("Enter lecturer address:");
            lecturer.Address = Console.ReadLine();
            Console.WriteLine("Enter lecturer phone number:");
            lecturer.Phone = Console.ReadLine();
            Console.WriteLine("Enter lecturer email:");
            lecturer.Email = Console.ReadLine();
            Console.WriteLine("Enter lecturer PPSN:");
            lecturer.PPSN = Console.ReadLine();
            Console.WriteLine("Enter lecturer ID:");
            lecturer.ID = Console.ReadLine();
            Console.WriteLine("Enter salary:");
            lecturer.Salary = decimal.Parse(Console.ReadLine());
            Console.WriteLine("Enter subject taught:");
            lecturer.SubjectTaught = Console.ReadLine().ToLower();
            lecturers.Add(lecturer);
        }

        public static void SearchPerson()
        {
            int searchChoice = 0;
            int studentSearch = 0;
            int lecturerSearch = 0;
            Console.WriteLine("Press:");
            Console.WriteLine("1 to search for a student");
            Console.WriteLine("2 to search for a lecturer");
            int.TryParse(Console.ReadLine(), out searchChoice);

            switch (searchChoice)
            {
                //search students
                case 1:
                    Console.WriteLine("Press:");
                    Console.WriteLine("1 to search by name");
                    Console.WriteLine("2 to search by student number");
                    int.TryParse(Console.ReadLine(), out studentSearch);

                    switch (studentSearch)
                    {
                        case 1:
                            Console.WriteLine("Enter student name:");
                            string studentNameSearch = Console.ReadLine();

                            foreach (Student student in students)
                                //(int i = 0; i < students.Count; i++)
                            {
                                if (student.Name.Contains(studentNameSearch))
                                {
                                    Console.WriteLine(student.ToString());
                                    break;
                                }
                                else 
                                {
                                    Console.WriteLine("Student name not found");
                                }
                            }
                            break;

                        case 2:
                            int studentIDSearch;
                            Console.WriteLine("Enter student number:");
                            int.TryParse(Console.ReadLine(), out studentIDSearch);

                            for (int i = 0; i < students.Count; i++)
                            {
                                if (students[i].StudentID == studentIDSearch)
                                {
                                    Console.WriteLine(students[i].ToString());
                                }
                                else
                                {
                                    Console.WriteLine("Student number not found");
                                }
                            }
                            break;

                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                    break;
                //search lecturers
                case 2:
                    Console.WriteLine("Press:");
                    Console.WriteLine("1 to search by name");
                    Console.WriteLine("2 to search by course taught");
                    int.TryParse(Console.ReadLine(), out lecturerSearch);

                    switch (lecturerSearch)
                    {
                        case 1:
                            Console.WriteLine("Enter lecturer name:");
                            string lecturerNameSearch = Console.ReadLine();
                            for (int i = 0; i < lecturers.Count; i++)
                            {
                                if (lecturers[i].Name == lecturerNameSearch)
                                {
                                    Console.WriteLine(lecturers[i].ToString());
                                }
                                else
                                {
                                    Console.WriteLine("Lecturer name not found");
                                }
                            }
                            break;

                        case 2:
                            Console.WriteLine("Enter course taught:");
                            string lecturerSubjectSearch = Console.ReadLine().ToLower();
                            for (int i = 0; i < lecturers.Count; i++)
                            {
                                if (lecturers[i].SubjectTaught == lecturerSubjectSearch)
                                {
                                    Console.WriteLine(lecturers[i].ToString());
                                }
                                else
                                {
                                    Console.WriteLine("Subject not found");
                                }
                            }
                            break;
                        default:
                            Console.WriteLine("Invalid option selected");
                            break;
                    }
                    break;

                default:
                    Console.WriteLine("Invalid option selected");
                    break;
            }
        }

        public static void ShowStudents()
        {
            //sort list by name
            List<Student> SortedStudents = students.OrderBy(o => o.Name).ToList();

            foreach (Student student in SortedStudents)
            {
                Console.WriteLine(student);
            }
        }

        public static void ShowLecturers()
        {
            //sort list by name
            List<Lecturer> SortedLecturers = lecturers.OrderBy(o => o.Name).ToList();

            foreach (Lecturer lecturer in SortedLecturers)
            {
                Console.WriteLine(lecturer.Name);
            }
        }

        public static void ShowPayrollDetails()
        {
            Console.WriteLine("Enter lecturer name:");
            string lecturerNameSearch = Console.ReadLine();
            for (int i = 0; i < lecturers.Count; i++)
            {
                if (lecturers[i].Name == lecturerNameSearch)
                {
                    Console.WriteLine(lecturers[i].PayrollDetails());
                }
                else
                {
                    Console.WriteLine("Lecturer name not found");
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个

            Boolean found = false;
            foreach (Student student in students)
            //(int i = 0; i < students.Count; i++)
            {
                if (student.Name.Contains(studentNameSearch))
                {
                    Console.WriteLine(student.ToString());
                    found = true;
                    break;
                }
            }
            if(found == false)
            {
                    Console.WriteLine("Student name not found");
            }​

答案 1 :(得分:1)

您的代码会为列表中的每个元素student输出“找不到学生名称”

  • 不满足条件student.Name.Contains(studentNameSearch)
  • 在列表中满足条件的第一个元素之前(如果存在这样的元素,否则将为列表中的每个元素打印消息)。

在搜索整个列表后,您只能确定没有符合条件的学生。

可能的解决方案:

Student studentMatch = null;
foreach (Student student in students)
{
    if (student.Name.Contains(studentNameSearch))
    {
         studentMatch = student;
         break;
    }
}      
if (studentMatch == null)
{
     Console.WriteLine("Student name not found");
}
else
{
    Console.WriteLine(studentMatch.ToString());
}