繁体   English   中英

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

[英]c# How to display an array based on user input

我有一个程序,用户在其中输入“通讯录”,在该程序中必须输入5个人才能进行搜索。 对于电话号码,它只使用区号以使其保持快速。

我的问题是,在程序末尾,它要求用户搜索名称或电子邮件,并返回其所在的索引。 我希望它显示该人的实际信息。

        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]);

您应该将此方法分解为更短的方法,因为它目前有很多功能(职责很多)并且难以阅读和理解。 第一步可以是将填充数据结构(输入数据)与使用它们(打印过滤后的数据)分开。

而不必为每个属性数组,你可以创建一个名为如类Person ,成员姓名,年龄,地址,电话,电子邮件和有一个单一的阵列-数组Person对象。 这样可以简化您的代码。

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

另外,请考虑如何处理“个人”的某些字段未输入的情况。 (您想使用例如空字符串还是完全不希望将填充一半的实例添加到列表中?)。

您应该使用具有名称,年龄,地址,电话号码属性的地址簿类,并且可以使用通用列表来存储用户输入的数据。

通用列表已经有查找方法。

更改

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之前,我不会不停下来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM