这是我的代码。它用于检查文本字符串是否是另一个文本字符串的子字符串。
public class Problem2
{
public boolean isSubstring(String T, String P)
{
System.out.println("String T: " + T);
System.out.println("String P: " + P);
char[] arrayT = T.toCharArray();
System.out.println("Array T: " + Arrays.toString(arrayT));
System.out.println("Array T length: " + arrayT.length);
char[] arrayP = P.toCharArray();
System.out.println("Array P: " + Arrays.toString(arrayP));
System.out.println("Array P length: " + arrayP.length);
int consecutive = 0;
boolean isTrue = false;
for (int i = 0; i < arrayT.length; i++)
{
System.out.println("i: " + i);
System.out.println("Consecutive characters before comparison: " + consecutive);
System.out.println("Letters in T left to be compared: " + (arrayT.length - i));
System.out.println("Unmatched letters in array P: " + (arrayP.length - consecutive));
if(arrayT.length - i < arrayP.length - consecutive)
{
System.out.println("isTrue: " + isTrue);
System.out.println("Break #1");
break;
}
System.out.println("arrayT[i]: " + arrayT[i]);
System.out.println("arrayP[consecutive]: " + arrayP[consecutive]);
if(arrayT[i] == arrayP[consecutive])
{
consecutive += 1;
}
else
{
consecutive = 0;
}
System.out.println("Consecutive characters after comparison: " + consecutive);
if(consecutive == arrayP.length)
{
System.out.println("Consecutive = Array P length, " + consecutive + " = " + arrayP.length);
isTrue = true;
System.out.println("isTrue: " + isTrue);
System.out.println("Break #2");
break;
}
}
return isTrue;
}}
以下是我的测试结果。正如下面将会看到的那样,每个测试在实际断开并返回布尔值之前循环两次for循环。为什么会发生这种情况,我该如何解决?任何人的帮助将不胜感激。
Test 1
String T: MINNESOTA
String P: INNE
Array T: [M, I, N, N, E, S, O, T, A]
Array T length: 9
Array P: [I, N, N, E]
Array P length: 4
i: 0
Consecutive characters before comparison: 0
Letters in T left to be compared: 9
Unmatched letters in array P: 4
arrayT[i]: M
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 1
Consecutive characters before comparison: 0
Letters in T left to be compared: 8
Unmatched letters in array P: 4
arrayT[i]: I
arrayP[consecutive]: I
Consecutive characters after comparison: 1
i: 2
Consecutive characters before comparison: 1
Letters in T left to be compared: 7
Unmatched letters in array P: 3
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 2
i: 3
Consecutive characters before comparison: 2
Letters in T left to be compared: 6
Unmatched letters in array P: 2
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 3
i: 4
Consecutive characters before comparison: 3
Letters in T left to be compared: 5
Unmatched letters in array P: 1
arrayT[i]: E
arrayP[consecutive]: E
Consecutive characters after comparison: 4
Consecutive = Array P length, 4 = 4
isTrue: true
Break #2
//Why does it break here, but continue to loop once again below before returning the boolean?
String T: MINNESOTA
String P: INNE
Array T: [M, I, N, N, E, S, O, T, A]
Array T length: 9
Array P: [I, N, N, E]
Array P length: 4
i: 0
Consecutive characters before comparison: 0
Letters in T left to be compared: 9
Unmatched letters in array P: 4
arrayT[i]: M
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 1
Consecutive characters before comparison: 0
Letters in T left to be compared: 8
Unmatched letters in array P: 4
arrayT[i]: I
arrayP[consecutive]: I
Consecutive characters after comparison: 1
i: 2
Consecutive characters before comparison: 1
Letters in T left to be compared: 7
Unmatched letters in array P: 3
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 2
i: 3
Consecutive characters before comparison: 2
Letters in T left to be compared: 6
Unmatched letters in array P: 2
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 3
i: 4
Consecutive characters before comparison: 3
Letters in T left to be compared: 5
Unmatched letters in array P: 1
arrayT[i]: E
arrayP[consecutive]: E
Consecutive characters after comparison: 4
Consecutive = Array P length, 4 = 4
isTrue: true
Break #2
INNE is a substring of MINNESOTA: true
Test 2
String T: MINNESOTA
String P: INEN
Array T: [M, I, N, N, E, S, O, T, A]
Array T length: 9
Array P: [I, N, E, N]
Array P length: 4
i: 0
Consecutive characters before comparison: 0
Letters in T left to be compared: 9
Unmatched letters in array P: 4
arrayT[i]: M
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 1
Consecutive characters before comparison: 0
Letters in T left to be compared: 8
Unmatched letters in array P: 4
arrayT[i]: I
arrayP[consecutive]: I
Consecutive characters after comparison: 1
i: 2
Consecutive characters before comparison: 1
Letters in T left to be compared: 7
Unmatched letters in array P: 3
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 2
i: 3
Consecutive characters before comparison: 2
Letters in T left to be compared: 6
Unmatched letters in array P: 2
arrayT[i]: N
arrayP[consecutive]: E
Consecutive characters after comparison: 0
i: 4
Consecutive characters before comparison: 0
Letters in T left to be compared: 5
Unmatched letters in array P: 4
arrayT[i]: E
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 5
Consecutive characters before comparison: 0
Letters in T left to be compared: 4
Unmatched letters in array P: 4
arrayT[i]: S
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 6
Consecutive characters before comparison: 0
Letters in T left to be compared: 3
Unmatched letters in array P: 4
isTrue: false
Break #1
//Why does it break here, but continue to loop once again below before returning the boolean?
String T: MINNESOTA
String P: INEN
Array T: [M, I, N, N, E, S, O, T, A]
Array T length: 9
Array P: [I, N, E, N]
Array P length: 4
i: 0
Consecutive characters before comparison: 0
Letters in T left to be compared: 9
Unmatched letters in array P: 4
arrayT[i]: M
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 1
Consecutive characters before comparison: 0
Letters in T left to be compared: 8
Unmatched letters in array P: 4
arrayT[i]: I
arrayP[consecutive]: I
Consecutive characters after comparison: 1
i: 2
Consecutive characters before comparison: 1
Letters in T left to be compared: 7
Unmatched letters in array P: 3
arrayT[i]: N
arrayP[consecutive]: N
Consecutive characters after comparison: 2
i: 3
Consecutive characters before comparison: 2
Letters in T left to be compared: 6
Unmatched letters in array P: 2
arrayT[i]: N
arrayP[consecutive]: E
Consecutive characters after comparison: 0
i: 4
Consecutive characters before comparison: 0
Letters in T left to be compared: 5
Unmatched letters in array P: 4
arrayT[i]: E
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 5
Consecutive characters before comparison: 0
Letters in T left to be compared: 4
Unmatched letters in array P: 4
arrayT[i]: S
arrayP[consecutive]: I
Consecutive characters after comparison: 0
i: 6
Consecutive characters before comparison: 0
Letters in T left to be compared: 3
Unmatched letters in array P: 4
isTrue: false
Break #1
INEN is a substring of MINNESOTA: false
以下是测试用例。
public class Problem2Test
{
/**
* Test of isSubstring method, of class Problem2.
*/
@Test
public void testIsSubstring()
{
System.out.println("Test 1");
String T = "MINNESOTA";
String P = "INNE";
Problem2 instance = new Problem2();
boolean expResult = true;
boolean result = instance.isSubstring(T, P);
System.out.println(P + " is a substring of " + T + ": " + instance.isSubstring(T, P));
assertEquals(expResult, result);
System.out.println("Test 2");
T = "MINNESOTA";
P = "INEN";
Problem2 instance2 = new Problem2();
expResult = false;
result = instance.isSubstring(T, P);
System.out.println(P + " is a substring of " + T + ": " + instance.isSubstring(T, P));
assertEquals(expResult, result);}}
答案 0 :(得分:0)
因为您为“测试1”致电instance.isSubstring(T,P)
两次 - 一次保存result
变量,一次保存在System.out
电话中。