0

これが私のコードです。テキストの文字列が別のテキストの文字列の部分文字列であるかどうかを確認するためのものです。

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 ループを 2 回循環してから、実際にブール値をブレークして返します。なぜこれが起こり、どうすれば修正できますか? 誰の助けも大歓迎です。

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

1 に答える 1

0

instance.isSubstring(T,P)「テスト 1」を 2 回呼び出しているためです。result変数を保存するために 1 回、System.out呼び出しで 1 回です。

于 2013-04-10T16:58:01.963 に答える