英文:
i want to do a app that runs thru the words from a txt file and search for a specific word and my foreach compare only the last word
问题
我在foreach
循环中有一个if
语句,只有当我搜索的单词是最后一个单词时才返回true
,我不知道该怎么办。
public Text InputNrDeImt;
public Text status;
public string rbd;
public string[] bd;
public string path;
public string NrDeImt;
public void OnCheck(){
path = Application.persistentDataPath + "/BazaDeDate.txt";
StreamReader reader = new StreamReader(path);
rbd = reader.ReadToEnd();
reader.Close();
bd = rbd.Split('\n');
NrDeImt = InputNrDeImt.text.ToString();
foreach(string word in bd){
if(NrDeImt == word.Trim()){
status.text = "找到了";
break; // 如果找到了就退出循环
}
else{
status.text = "不在数据库中";
}
}
}
文件看起来像这样:
aaa
aab
aba #我正在寻找的单词
abb
baa
bab
bba
bbb
我的代码只有在单词是"bbb"时才能找到它。
英文:
I have an if
in a foreach
and it returns true
only if the word I'm searching is the last one and I don't know what to do
public Text InputNrDeImt;
public Text status;
public string rbd;
public string[] bd;
public string path;
public string NrDeImt;
public void OnCheck(){
path = Application.persistentDataPath +"/BazaDeDate.txt";
StreamReader reader=new StreamReader(path);
rbd= reader.ReadToEnd();
reader.Close();
bd=rbd.Split("\n"[0]);
NrDeImt = InputNrDeImt.text.ToString();
foreach(string word in bd){
if(NrDeImt==word){
status.text= "found";
}
else{
status.text= "Nu este in baza";
}
}
}
the file looks like that
aaa
aab
aba #the word i was looking for
abb
baa
bab
bba
bbb
and my code only finds the word if it's "bbb"
答案1
得分: 3
Your foreach loop will check every word, meaning that it won't stop after it has found the correct word, in short:
aaa // status.text= "Nu este in baza";
aab // status.text= "Nu este in baza";
aba // status.text= "found";
abb // status.text= "Nu este in baza";
baa // status.text= "Nu este in baza";
bab // status.text= "Nu este in baza";
bba // status.text= "Nu este in baza";
bbb // status.text= "Nu este in baza";
So unless you were looking for the last word, as soon as it loops through the last word, the status is updated to the "not found" text again.
You can fix it by breaking the loop as soon as you found your result. And for performance, only set the text once:
status.text = "Nu este in baza"; // default text is not found
foreach(string word in bd){
if(NrDeImt==word){
status.text= "found"; // Now set the found text
break; // stop the loop, we found the text
}
}
英文:
Your foreach loop will check every word, meaning that it won't stop after it has found the correct word, in short:
aaa // status.text= "Nu este in baza";
aab // status.text= "Nu este in baza";
aba // status.text= "found";
abb // status.text= "Nu este in baza";
baa // status.text= "Nu este in baza";
bab // status.text= "Nu este in baza";
bba // status.text= "Nu este in baza";
bbb // status.text= "Nu este in baza";
So unless you were looking for the last word, as soon as it loops through the last word, the status is updated to the "not found" text again.
You can fix it by breaking the loop as soon as you found your result. And for performance, only set the text once:
status.text = "Nu este in baza"; // default text is not found
foreach(string word in bd){
if(NrDeImt==word){
status.text= "found"; // Now set the found text
break; // stop the loop, we found the text
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论