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



我有一个程序,用户在其中输入一个"地址簿",必须在其中输入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的类,该类的成员为Name、Age、Address、Phone、Email,而不是每个属性都有一个数组,并且只有一个数组-Person对象的数组。这将简化您的代码。

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

此外,考虑一下当Person的某些字段未输入时如何处理这种情况。(您是想使用例如空字符串,还是根本不想将半填充的实例添加到列表中…).

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

泛型列表已具有查找方法。

更改

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之前,我不会麻烦地停止。

相关内容

  • 没有找到相关文章

最新更新