Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:根据位置和使用Spanable比较单词_Android_Arrays_For Loop_Contains_Spannable - Fatal编程技术网

Android:根据位置和使用Spanable比较单词

Android:根据位置和使用Spanable比较单词,android,arrays,for-loop,contains,spannable,Android,Arrays,For Loop,Contains,Spannable,我这里有一个代码,可以改变句子中单词的颜色。如果找到的单词位于同一位置,则为紫色。如果答案包含不同位置的单词,则为黄色;如果未找到该单词,则为红色 我现在的问题是,即使单词的位置不同,颜色也会变成紫色。我还尝试使用splitInput[I].containssplitAnswer[I]将单词更改为黄色,但我得到了一个重复的单词,例如,这是一个示例句子 String answer = "This is a sample sentence" String userIn

我这里有一个代码,可以改变句子中单词的颜色。如果找到的单词位于同一位置,则为紫色。如果答案包含不同位置的单词,则为黄色;如果未找到该单词,则为红色

我现在的问题是,即使单词的位置不同,颜色也会变成紫色。我还尝试使用splitInput[I].containssplitAnswer[I]将单词更改为黄色,但我得到了一个重复的单词,例如,这是一个示例句子

        String answer = "This is a sample sentence"
        String userInput = "It was a sample sentence"
        boolean wordFound = false;
        String[] splitAnswer = answer.split(" ");
        String[] splitInput = userInput.split(" ");

        for (int i=0; i<splitAnswer.length;i++) 
        {
            for (int j=0;j<splitInput.length;j++) 
            {
                if(splitInput[i].equalsIgnoreCase(splitAnswer[i])) 
                {
                wordFound = true;
                //color the word to violet
                }
            {
            if(wordFound==false)
            {
            //color the word to red 
            }
             //display the sentence
             wordFound == false; 
        }

你好很抱歉反应太晚。我上次尝试了该代码,得到了一条空指针异常错误消息。顺便说一句,userInput不是常量,数组长度可能会根据用户输入而改变。你能帮我解决这个问题吗?
String answer = "This is a sample sentence";
        String userInput = "It was a sample sentence";
        boolean wordFound = false;
        String[] splitAnswer = answer.split(" ");
        String[] splitInput = userInput.split(" ");

         for (int i=0; i<splitAnswer.length;i++) 
         {

        if (splitInput[i].equalsIgnoreCase(splitAnswer[i]))
        {

            System.out.println ("Word found");
        }
        else if(!wordFound)
        {

            System.out.println ("Word Not found");
        }
    }