【问题标题】:problem with get the user input and save in array in c#在c#中获取用户输入并保存在数组中的问题
【发布时间】:2020-03-19 23:14:29
【问题描述】:

我写了一个简单的代码,我的程序需要两个输入,一个是姓名,第二个是号码(电话号码)。 我写了一节来查找联系人。 当用户输入 1 时,他应该添加姓名和电话号码,然后用户可以输入 1 或 2(1 用于添加联系人,2 用于搜索)当用户再次输入 1 时,所有旧日期以及删除,我不想要这个请帮忙。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project_01
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = new string[5];
            string[] phonenumb = new string[5];
            bool itsTrue;
            //int counter;

            string help = @"enter 1 for add contact.
enter 2 for exit.0
enter 3 for search";
            Console.WriteLine(help);
            while (true)
            {
                Console.Write("=>");
                int input = Convert.ToInt32(Console.ReadLine());
                if (input == 1)
                {

                    for (int i = 0; i < names.Length;)
                    {

                        Console.Write("name : ");
                        names[i] = Console.ReadLine();
                        Console.Write("number : ");
                        phonenumb[i] = Console.ReadLine();
                        i++;
                        break;                        
                    }

                }
                else if (input == 2)
                {
                    Console.Write("enter y for exir and n to stay: ");
                    string exit = Console.ReadLine().ToUpper();
                    if (exit == "Y")
                    {

                        break;
                    }
                    else if (exit == "N")
                    {
                        continue;
                    }

                }
                else if (input == 3)
                {
                    Console.Write("enter the name: ");
                    string search = Console.ReadLine();
                    itsTrue = false;
                    for (int j = 0; j < names.Length; j++)
                    {
                        if (search == names[j])
                        {
                            Console.WriteLine("name: {0}", names[j]);
                            Console.WriteLine("number:{0}", phonenumb[j]);
                            itsTrue = true;
                            break;
                        }

                    }
                    if (!itsTrue)
                    {
                        Console.WriteLine("content not found.");
                    }

                }
            }
        }
    }
}

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    让我们逐步检查菜单选项 1 的代码:

    for (int i = 0; i < names.Length;)
    {                        
        Console.Write("name : ");
        names[i] = Console.ReadLine();
        Console.Write("number : ");
        phonenumb[i] = Console.ReadLine();
        i++;
        break;                        
    }
    

    它以 i 设置为 0 开始一个循环,并在 i &lt; names.Length 时循环。在循环的第一次迭代中,它将names[i](即names[0])分配给从控制台读取的值,然后是电话号码。然后i 递增到1,循环随即中断。

    现在想象下一次按下1。它仍在运行完全相同的代码。循环以i 为0 开始,分配给names[0]phonenumb[0],然后跳出循环。不是你想要的。

    您想要一个循环来添加新行吗?如果是这样,为什么?它是否不止一次地做某事?不,它只应该做一件事。请注意,即使使用循环,您也总是在第一次迭代时跳出循环,因此它甚至不是循环。

    缺少的部分是您每次都希望在不同的索引处写入namesphonenumb。为此,您需要有一个在菜单循环的每次迭代中都存在的变量。如果您在菜单循环之外声明 int insertIndex = 0; 之类的内容,则可以将插入逻辑更改为以下内容:

    Console.Write("name : ");
    names[insertIndex] = Console.ReadLine();
    Console.Write("number : ");
    phonenumb[insertIndex] = Console.ReadLine();
    insertIndex++;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多