Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试将对象传递到数组,然后搜索该数组以返回该对象_C#_Arrays_Oop_Multidimensional Array - Fatal编程技术网

C# 尝试将对象传递到数组,然后搜索该数组以返回该对象

C# 尝试将对象传递到数组,然后搜索该数组以返回该对象,c#,arrays,oop,multidimensional-array,C#,Arrays,Oop,Multidimensional Array,因此,我试图创建一个程序,允许我将对象信息输入数组,然后搜索该数组中的对象片段并返回该对象 在这一点上,我不确定我的对象是否被放入数组,我的调用是否正确 理想情况下,我希望填充对象数组,然后在菜单显示时选择搜索选项。我将搜索匹配的对象数组,并显示匹配的对象 我不确定我的错误在哪里,因为这没有返回我的搜索选项。如果你能找到,那就太好了。谢谢大家! static void Main(string[] args) { Greeting(); WriteLine(

因此,我试图创建一个程序,允许我将对象信息输入数组,然后搜索该数组中的对象片段并返回该对象

在这一点上,我不确定我的对象是否被放入数组,我的调用是否正确

理想情况下,我希望填充对象数组,然后在菜单显示时选择搜索选项。我将搜索匹配的对象数组,并显示匹配的对象

我不确定我的错误在哪里,因为这没有返回我的搜索选项。如果你能找到,那就太好了。谢谢大家!

static void Main(string[] args)
    {
        Greeting();
        WriteLine();
        School [] newStudent = new School[4];
        string firstName;
        string lastName;
        string major;
        int id;
        double gpa;
        for (int i = 0; i < newStudent.Length; i++)
        {
            GetInfo(out firstName, out lastName, out major);
            id = GetId();
            gpa = GetGpa();
            WriteLine();
            newStudent[i] = new School(lastName, firstName, major, id, gpa);
        }
        DisplayMenu();
        int menuOpt = GetChoice();
        DoChoice(menuOpt, newStudent);


    }

    static void Greeting()
    {
        WriteLine("Student Input");
    }

    static void GetInfo(out string lastn, out string firstn, out string mjr)
    {
        Write("Enter student's last name: ");
        lastn = ReadLine();
        Write("Enter student's first name: ");
        firstn = ReadLine();
        Write("Enter student's major: ");
        mjr = ReadLine();
    }

    static int GetId()
    {
        Write("Enter student's ID number: ");
        int inum = int.Parse(ReadLine());
        while (inum <= 0)
        {
            Write("ID number must be greater than 0. Please enter student's ID number: ");
            inum = int.Parse(ReadLine());
        }
        return inum;
    }

    static double GetGpa()
    {
        Write("Enter student's GPA: ");
        double sgpa = double.Parse(ReadLine());
        while (sgpa < 0.0 || sgpa > 4.0)
        {
            Write("That's not a valid GPA. Try Again: ");
            sgpa = double.Parse(ReadLine());
        }
        return sgpa;
    }

    static void DisplayMenu()
    {
        WriteLine("1. Search by Last Name");
        WriteLine("2. Search by First Name");
        WriteLine("3. Search by Major");
        WriteLine("4. Quit");
    }

    static int GetChoice()
    {
        Write("What is your choice? ");
        int select = int.Parse(ReadLine());
        while (select < 1 || select > 4)
        {
            Write("That's not a valid choice. Try Again: ");
            select = int.Parse(ReadLine());
        }
        return select;
    }

    static void DoChoice(int choice, params School [] nStudent)
    {
        switch (choice)
        {
            case 1:
                FindLastName(nStudent);
                break;
            case 2:
                break;
            //more options will go here at a later date with their matching methods like case 1

        }
    }

    static void FindLastName(params School [] findLName)
    {
        Write("What is the last name? ");
        string findL = ReadLine();
        int pos = Array.IndexOf(findLName, findL);
        if (pos > -1)
        {
            PrintStudent(findLName[pos]);
        }
        else
        {
            Write("No results found with that last name {0}.", findL);
        }
    }

    static void PrintStudent(School student1)
    {
        Write(student1.ToString());
    }
更新!:所以我已经确认我的数组正在填充我的对象,但是我想知道这些值是否被正确地传递给我的方法。 当前尝试使用my FindLastName()方法搜索对象数组时,仅返回else语句,就好像数组中的值不存在一样

更新2:在做了一些建议的修改之后,我大约80%相信我的错误在这个方法中

static void FindLastName(params School [] findLName)
    {
        for (int i = 0; i < findLName.Length; i++)
        {
            WriteLine(findLName[i]);

        }
        // The above loop proves that the objects exist within the array after being passed to the method.

        Write("What is the last name? ");
        string findL = ReadLine();
        School foundStudent = null;
        for (int i = 0; i < findLName.Length; i++)
        {
            if (findLName[i].Lname == findL)
            {
                foundStudent = findLName[i];
                break;
            }
        }
        if (foundStudent != null)
        {
            PrintStudent(foundStudent);
        }
        else
        {
            Write("No results found with that last name {0}.", findL);
        }
        // When I run this it returns the else statement even though I am using a last name that exists within the array
static void FindLastName(params School[]findLName)
{
for(int i=0;i
根据到目前为止所学的概念,您可能希望在搜索算法中稍微更改一下逻辑

  • 您可以做的是创建一个布尔变量来跟踪是否找到任何学生,并以
    false
  • 然后在数组中循环,每次一个学生,并将姓氏与我们正在搜索的姓氏进行比较
  • 如果我们找到学生,输出他们的详细信息,并将布尔变量设置为
    true
  • 在循环之外,我们现在可以检查我们的变量是否为false,如果为false,我们就可以写下没有找到学生的消息:
  • 使用
    IndexOf
    无法正常工作的原因是必须传递数组中存储的类型的对象,并且必须与数组中的对象完全匹配(类的
    Compare
    方法将返回true)

    static void FindLastName(params School[]findLName)
    {
    写(“姓什么?”);
    字符串findL=ReadLine();
    //创建一个变量以跟踪是否找到匹配项
    bool foundStudent=false;
    //搜索每个数组项
    for(int i=0;i
    您可以利用LINQ,通过以下方式使此搜索变得轻松愉快:

    string searchValue = PutSearchValueInHere();
    School[] selectedStudentArray = schoolArray.Where(school=>school.SearchField == searchValue).ToArray();
    School selectedStudent = selectedStudentArray[0]; //Assuming you used a primary key as your search field
    
    基本上,这是如何工作的,您选择一个字段(可能是唯一的学生ID)和类的一个属性(例如
    .StudentID
    ),然后将其作为lambda表达式传递到数组的扩展方法
    。其中
    。然后它将返回lambda计算为
    true
    的任何项

    表达式
    school=>school.SearchField==searchValue
    本质上意味着“搜索值是否等于此school对象的SearchField,是/否?”并对数组中的每个项重复此操作

    接下来,
    .ToArray()
    简单地告诉它将结果存储为一个新数组

    完成此操作后,假设只有一个学生符合条件,您可以自由选择数组中的第一项。但是,可能需要检查此项是否为空


    事后想了想,我真的忍不住觉得应该将学生信息存储在SQL数据库中,并通过ODBC链接访问它,但这完全是另一回事。

    在我看来,您似乎正在将参数传递给
    GetInfo()
    以错误的顺序,最终会将此人的名字与姓氏交换。因此,稍后,当您按姓氏搜索某人时,您将永远无法找到匹配项

    函数签名如下所示(以
    lastName
    作为第一个参数):

    但是您这样调用它(第二个参数是
    lastName
    ):

    因此,要解决这个问题,您只需交换上一行中
    firstName
    lastName
    的位置

    <>也,我注意到,你试图把GPA格式化成一个日期,这是行不通的。你可以考虑改变这一行:

    gpa.ToString("D2")
    
    若要显示小数点后一位,请执行以下操作:


    您是否有实际问题,或者您只是想查看一下您的代码?但我不会使用全名索引,因为您假设用户输入了全名-您的问题是如何改进代码的搜索部分?目前,当我搜索我的数组时,什么都没有
    string searchValue = PutSearchValueInHere();
    School[] selectedStudentArray = schoolArray.Where(school=>school.SearchField == searchValue).ToArray();
    School selectedStudent = selectedStudentArray[0]; //Assuming you used a primary key as your search field
    
    static void GetInfo(out string lastn, out string firstn, out string mjr)
    
    GetInfo(out firstName, out lastName, out major);
    
    gpa.ToString("D2")
    
    gpa.ToString("0.0")