我正在比较两个字符串数组,我想在下面的代码中得到多少是正确和不正确的。感谢帮助。
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;
}
答案 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而不是等于