在字符串数组的比较中获取正确和错误答案的计数是错误的

时间:2013-06-12 13:47:29

标签: android count compare arrays

我正在比较两个字符串数组,我想在下面的代码中得到多少是正确和不正确的。感谢帮助。

                 boolean foundtext = false;

            //outer loop for all the elements in arrayA[i]
            for(int j = 0; j < array1.length; j++)
            {
                //System.out.println("ArrData" + (array1[j]));
            //inner loop for all the elements in arrayB[j]
            for (int k = 0; k < compare1.length;k++)
            {   //System.out.println("FieData" + (i1[k])); 
            //compare arrayA to arrayB and output results
            if( array1[j].equals(compare1[k]))
            {
            foundtext = true;
                //return j;
            System.out.println( "arrayA element \"" + array1[j] + "\" was found in arrayB" );
            //System.out.println("Correct" + correctCount);
            } 
            }
            if (foundtext == false)
            {
            System.out.println( "arrayA element \"" + array1[j] + "\" was Not found in arrayB" );
            //System.out.println("InCorrect" + incorrectCount);
            }
            //set foundtext bool back to false
            foundtext = false;
            }

2 个答案:

答案 0 :(得分:0)

在循环外声明两个int:

boolean foundtext = false;
int correct = 0;
int incorrect =0;

        //outer loop for all the elements in arrayA[i]
        for(int j = 0; j < array1.length; j++)
        {
            //System.out.println("ArrData" + (array1[j]));
        //inner loop for all the elements in arrayB[j]
        for (int k = 0; k < compare1.length;k++)
        {   //System.out.println("FieData" + (i1[k])); 
        //compare arrayA to arrayB and output results
        if( array1[j].equals(compare1[k]))
        {
        foundtext = true;
        correct++;
            //return j;
        System.out.println( "arrayA element \"" + array1[j] + "\" was found in arrayB" );
        //System.out.println("Correct" + correctCount);
        } 
        }
        if (foundtext == false)
        {
        System.out.println( "arrayA element \"" + array1[j] + "\" was Not found in arrayB" );
        incorrect++;
        //System.out.println("InCorrect" + incorrectCount);
        }
        //set foundtext bool back to false
        foundtext = false;
        }
        System.out.println("Correct" + correct);
        System.out.println("Incorrect" + incorrect);

答案 1 :(得分:-1)

for String compairing使用equalIgnorecase而不是等于