c# 如何根据用户输入显示数组

c# How to display an array based on user input

我有一个程序,用户在其中输入 "Address book" 他们必须输入 5 个人才能搜索。对于 phone 个号码,它只处理区号以保持快速。

我的问题是,在程序结束时,它要求用户搜索姓名或电子邮件,returns 它所在的索引。我希望它显示那个人的实际信息。

        static void Main (string[] args)
    {
        int size = 5;
        string[] names = new string[size];
        int[] Age = new int[size];
        string[] address = new string[size];
        int[] phone = new int[size];
        string[] email = new string[size];
        bool stop = false; // stop to flag for while loops to end loops
        string temp;
        int counter = 0;
        while((stop == false) && (counter <5)) // counter can only get to 5 and both statements must be true
        {
            Console.WriteLine ("Enter a name or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                names [counter] = temp; // temp value stored into names array [counter]
            }
            Console.WriteLine ("Enter the age or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else 
            {
                Age [counter] = Convert.ToInt16(temp); // converts a string to integer
            }
            Console.WriteLine ("Enter address or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                address [counter] = temp;
            }
            Console.WriteLine ("Enter phone number or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else 
            {
                phone [counter] = Convert.ToInt32(temp); // converts a string to integer

            }
            Console.WriteLine ("Enter a email or q/Q to quit");
            temp = Console.ReadLine ();
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true; // Allowing user to quit
            } 
            else 
            {
                email [counter] = temp;
                counter = counter + 1;
            }
        }       
        for (int i = 0; i <5; i++)
        {
            Console.WriteLine (names [i]);
            Console.WriteLine (Age [i]);
            Console.WriteLine (address [i]);
            Console.WriteLine (phone [i]);
            Console.WriteLine (email [i]);
        } 
        Console.WriteLine ("Enter a name or email to search or Q/q to quit");
        temp = Console.ReadLine ();
        stop = false; 
        while (stop == false) 
        {
            if (temp == "q" || temp == "Q") {
                Console.WriteLine ("Quitting");
                stop = true;
            } 
            else
            {
                for (int i = 0; i <5; i++)
                {
                    if (temp == names [i] || temp == email [i]) {
                        Console.WriteLine ("Name is found at index {0}", i);
                        stop = true;
                    } 
                    else
                    {
                        if (i == 5) 
                        {
                            Console.WriteLine ("Value not found");
                            stop = true;
                        }

                } 
            }
        }
        Console.ReadLine ();

只需更换

Console.WriteLine ("Name is found at index {0}", i);

Console.WriteLine ("Person is found at index {0}. Name: {1}, Age: {2}, Address: {3}, Phone: {4}, Email: {5}", i, names[i], Age[i], address[i], phone[i], email [i]);

你应该把上面的这个方法分解成更短的方法,因为目前它做了很多事情(它有很多责任)并且难以阅读和理解。第一步可能是将填充数据结构(输入数据)与使用它们(打印过滤数据)分开。

您可以创建 class,而不是为每个属性创建数组,例如Person 具有成员姓名、年龄、地址、Phone、电子邮件并具有单个数组 - Person 对象的数组。那会简化你的代码。

最好将 Persons 保留在列表中而不是数组中,因为您事先不知道用户何时会点击 Q)uit 并停止添加新的 Persons。如果大小为 10000 并且用户在 10 个条目后退出,则您为 Person 对象分配了 9990 个未使用的空间。

另外想想Person的某些字段没有填写的情况下怎么处理。 (您是想使用例如空字符串,还是根本不想将半填充实例添加到列表中...)。

你应该使用地址簿 class 具有姓名、年龄、地址、phone 数字属性,你可以使用通用列表来存储用户输入的数据。

Generic List 已有查找方法。

改变

if (temp == names [i] || temp == email [i]) {
    Console.WriteLine ("Name is found at index {0}", i);
    stop = true;
}

if (temp == names [i] || temp == email [i]) {
    Console.WriteLine (names [i]);
    Console.WriteLine (Age [i]);
    Console.WriteLine (address [i]);
    Console.WriteLine (phone [i]);
    Console.WriteLine (email [i]);
}

直到用户输入 q 或 Q,我才会停下来。