【问题标题】:Searching an Array using a while loop in C#?在 C# 中使用 while 循环搜索数组?
【发布时间】:2014-01-10 09:55:20
【问题描述】:

我正在尝试使用 while 循环搜索数组,使用用户搜索输入,此时我已将一个单词电影标题列出到标准文本文件中,然后 while 循环继续搜索文件直到它找到了,然后在控制台上输出了。。但是我有个问题,操作符“==”不能应用于字符串变量?我将如何解决这个问题?非常感谢!圣诞快乐,这是我的代码:

        //Declare variables
        int iOneWordTitle= 0;
        string sSearch;

        //Declare array
        const int iFilm = 7;
        string[] sOneWordTitle = new string[iFilm];

        //Add heading to console
        Console.WriteLine("List of one word film titles");
        Console.WriteLine();

        //ask user what they want to search for
        Console.WriteLine("What film would you like to search for?");
        sSearch = Console.ReadLine();

        //Read the film names from the datafile
        using (StreamReader sr = new StreamReader("filmnames.txt"))
        {
            while (iOneWordTitle < iFilm)
            {
                sOneWordTitle[iOneWordTitle] = (sr.ReadLine());
                iOneWordTitle++;

                if (sSearch == sOneWordTitle)
                {
                    Console.WriteLine(sSearch + " was found at position " + iOneWordTitle);
                }

                else
                {
                    Console.WriteLine("The film was not found");
                }
            }

        }

【问题讨论】:

  • if (sSearch == sOneWordTitle) 将字符串与字符串 [] 进行比较。你可能想弄清楚你真正想在那里比较的东西。
  • 我想要这样,如果搜索到的就是电影,那么就输出到控制台上
  • 通过这样做,您将字符串与数组对象进行比较,而不是数组中包含的字符串。

标签: c# arrays while-loop console


【解决方案1】:
  1. 您可以使用File.ReadAllLines()轻松读取文本文件中的所有行,而不是使用StreamReader

  2. 您还打印了 未找到胶片 7 次。如果您希望只打印一次,您可以保留一个 bool 变量来指示它未找到。

  3. 您将文件中的标题数量预先配置为 7。如果文件中可以有更多标题怎么办?

所以,我想你可能想稍微改变一下你的代码:

        int iOneWordTitle = 0;
        string sSearch;

        //Add heading to console
        Console.WriteLine("List of one word film titles");
        Console.WriteLine();

        //ask user what they want to search for
        Console.WriteLine("What film would you like to search for?");
        sSearch = Console.ReadLine();

        //Read all titles from the text file
        string[] titles = File.ReadAllLines("filmnames.txt");

        //Search the array
        bool found = false;
        foreach (string title in titles)
        {
            if (title.Equals(sSearch))
            {
                Console.WriteLine(sSearch + " was found");
                found = true;
            }
        }

        //If the title wasn't found, print not found message
        if (!found)
            Console.WriteLine("The film was not found");

如果您仍想打印找到它的位置,您可以将 foreach 循环转换为常规循环:

        for (int iOneWordTitle = 0; iOneWordTitle < titles.Count(); iOneWordTitle++)
        {
            if (titles[iOneWordTitle].Equals(sSearch))
            {
                Console.WriteLine(sSearch + " was found at position " + iOneWordTitle);
                found = true;
            }
        }

【讨论】:

  • 谢谢,第一种方法似乎非常适合查找电影,但我有点困惑,虽然我将如何在其中实现这个 for 循环以找到电影在列表中的位置,可以你提供任何进一步的帮助?谢谢:)
【解决方案2】:

应该是

if (sSearch == sOneWordTitle[iOneWordTitle])

而不是

if (sSearch == sOneWordTitle)

在您的版本中,您将字符串与字符串数组进行比较,而不是数组索引值。

我可能还会考虑你的变量名。

【讨论】:

  • iOneWordTitle++; 需要移到比较下方,否则将在下一个(到目前为止未设置的)数组槽中进行比较。
  • 是的,那种解决方法,但现在它只是说当我输入正确的电影名称时没有找到电影,知道我可能哪里出错了吗?谢谢
  • 尝试将字符串比较为小写或使用String.EqualsStringComparison.CurrentCultureIgnoreCase
【解决方案3】:

它必须是这样的:

if (sSearch == sOneWordTitle[iOneWordTitle])

不要在这里这样做:

 if (sSearch == sOneWordTitle)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2015-02-20
    • 2011-12-03
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多