我正在尝试从一个句子中的单词中搜索几个特定字符串。最终这个句子将被用户输入,但我现在已经硬编码了,以便于测试。如果程序找到字符串,它应该返回“Yes”和“No”,如果没有。问题是,我一直都很满意。如何为特定字符串搜索字符串数组
public class main {
public static void main(String[]args)
{
String Sentence = "This is a sentence";
String[] CensorList =
{"big","head"};
String[] words = Sentence.split(" ");
System.out.println(words.length);
boolean match = false;
for(int i = 0; i < words.length; i++)
{
for (int j = 0; j < CensorList.length; j++)
{
if(words[i].equals(CensorList[j]))
{
match = true;
}else{
match = false;
}
}
}
if (match = true){
System.out.println("Yes");}
else{
System.out.println("No");
}
}}
我会很感激的任何帮助,这一个,在此先感谢。
没有检查你的代码,但你肯定想删除 ';'后if().. – 2013-04-25 13:20:59