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

时间:2015-04-07 22:27:31

标签: c# arrays console-application 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 ();

3 个答案:

答案 0 :(得分:1)

只需替换

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

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

您可以创建名为eg的类,而不是为每个属性创建数组。 Person成员名称,年龄,地址,电话,电子邮件和一个数组 - 一组Person个对象。这将简化您的代码。

最好将人员保持在列表而不是数组中,因为你事先不知道用户何时点击Q)uit并停止添加新人员。如果size为10000并且用户在10个条目之后退出,则为Person对象分配9990但未使用的空格。

另外,想想如果没有输入Person的某些字段时如何处理这种情况。 (你想使用例如空字符串,或者根本不想将半填充实例添加到列表中......)。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

更改

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之前,我不会打扰停止。