这是我想解决的问题:我有两个字符串数组(“匹配”和“可视对象”)。我想搜索数组“matches”中的所有单词,查看数组中是否至少有一个单词“visibleObjects”。如果满足这个条件,我想再次搜索“匹配”中的单词,这次要从数组“actionWords”中查找至少一个单词。这是我有,其中“TESTDIR”仅仅是被打印的调试字符串:比较字符串数组与嵌套循环的问题
protected void Action(){
boolean actionWord = false;
String target = null;
testDir = "first stage";
firstLoop:
for(String word : matches)
{
testDir += " " + word;
for(String hint : visibleObjects)
{
testDir += " " + hint;
if(word.equals(hint))
{
target = word; //found a matching word
testDir = "Hint found";
break firstLoop;
}
}
}
if(target != null)
{
testDir = "stage two";
secondLoop:
for(String word : matches)
{
for(String action : actionWords)
{
if(word.equals(action))
{
actionWord = true; //found one word from the actionWords array
testDir = "Acion OK";
break secondLoop;
}
}
}
}
if(actionWord){
testDir = target;
performAction(target);
}
}
我得到的印刷是从数组的数组比赛和所有单词的第一个字visibleObject一次,所以它不会通过第二个循环....
此编码是正确的?任何人都可以发现错误?
感谢您的帮助!
你说得对,代码是正确的。我正在测试Android VoiceRecogniser的功能。事实证明,所有识别的单词实际上都是作为一个长条目输入到matches []中,而不是一系列字符串,这就是为什么for循环不能正常工作....我在字符串I上使用“contains”方法从匹配[]数组创建,它工作正常....感谢您的输入! – Alex