0

我正在比较两个完成的字符串数组,我想在下面的代码中得到正确和不正确的数组。帮助表示赞赏。

                 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;
            }
4

2 回答 2

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);
于 2013-06-12T13:51:15.110 回答
-1

对于字符串比较使用 equalIgnorecase 而不是 equal

于 2013-06-12T14:03:49.467 回答