在C#中使用while循环搜索数组?
我正在尝试使用while循环搜索数组,使用用户搜索输入,在我将一个单词的电影标题列为标准文本文件的那一刻,while循环继续搜索该文件,直到找到为止,然后在控制台上输出。。但是我有一个问题,运算符“==”不能应用于字符串变量?我该如何解决这个问题?谢谢!圣诞快乐,这是我的密码:在C#中使用while循环搜索数组?,c#,arrays,while-loop,console,C#,Arrays,While Loop,Console,我正在尝试使用while循环搜索数组,使用用户搜索输入,在我将一个单词的电影标题列为标准文本文件的那一刻,while循环继续搜索该文件,直到找到为止,然后在控制台上输出。。但是我有一个问题,运算符“==”不能应用于字符串变量?我该如何解决这个问题?谢谢!圣诞快乐,这是我的密码: //Declare variables int iOneWordTitle= 0; string sSearch; //Declare array
//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");
}
}
}
//声明变量
int-iOneWordTitle=0;
字符串搜索;
//声明数组
常数int iFilm=7;
字符串[]sOneWordTitle=新字符串[iFilm];
//将标题添加到控制台
Console.WriteLine(“单字电影标题列表”);
Console.WriteLine();
//询问用户想要搜索什么
Console.WriteLine(“您想要搜索什么电影?”);
sSearch=Console.ReadLine();
//从数据文件中读取电影名称
使用(StreamReader sr=newstreamreader(“filmnames.txt”))
{
while(iOneWordTitle
应该是
if (sSearch == sOneWordTitle[iOneWordTitle])
而不是
if (sSearch == sOneWordTitle)
在您的版本中,比较字符串与字符串数组,而不是数组索引值
我可能还会考虑变量名。应该是
if (sSearch == sOneWordTitle[iOneWordTitle])
而不是
if (sSearch == sOneWordTitle)
在您的版本中,比较字符串与字符串数组,而不是数组索引值
我可能也会考虑变量名。它应该是这样的:
if (sSearch == sOneWordTitle[iOneWordTitle])
而不是在这里这样做:
if (sSearch == sOneWordTitle)
它应该是这样的:
if (sSearch == sOneWordTitle[iOneWordTitle])
而不是在这里这样做:
if (sSearch == sOneWordTitle)
file.ReadAllLines()
bool
变量以指示未找到它 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(int-iOneWordTitle=0;iOneWordTitle
file.ReadAllLines()
bool
变量以指示未找到它 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(int-iOneWordTitle=0;iOneWordTitle
if(sSearch==sOneWordTitle)
将字符串与字符串[]进行比较。您可能想找出您真正想要比较的内容。我希望这样,如果搜索等于胶片,那么要在控制台上输出它,通过这样做,您可以将字符串与数组对象进行比较,而不是与数组中包含的字符串进行比较。if(sSearch==sOneWordTitle)
将字符串与字符串[]进行比较。您可能想找出您真正想要比较的内容。我希望这样,如果搜索等于胶片,那么要在控制台上输出它,通过这样做,您可以将字符串与数组对象进行比较,而不是与数组中包含的字符串进行比较。iOneWordTitle++
需要移动到比较下方,或者比较将进行到下一个(目前为止未设置)阵列插槽。是的,这样可以解决问题,但现在它只是说当我键入正确的电影名称时,找不到电影,知道我可能出了什么错吗?ThanksTry将字符串比较为小写,或使用String.Equals
和StringComparison.CurrentCultureIgnoreCase
iOneWordTitle++需要移动到比较下方,或者比较将进行到下一个(目前为止未设置)阵列插槽。是的,这样可以解决问题,但现在它只是说当我键入正确的电影名称时,找不到电影,知道我可能出了什么错吗?Thanksry将字符串比较为小写,或者使用String.Equals
和StringComparison.CurrentCultureInoRecase
谢谢,第一种方法对于查找电影似乎非常有效,我有点困惑,我想知道如何在其中实现for循环以查找电影在列表中的位置,你能提供进一步的帮助吗?谢谢:)谢谢,第一种方法似乎很适合找到电影,我有点困惑,我该如何实现循环int