![](/img/trans.png)
[英]How can i get my While statement to correctly loop and break and at the same time input another option in java?
[英]Java: Why does my for loop cycle through one more time and then break after I try to break out of it the first time?
這是我的代碼。 用於檢查文本字符串是否為另一個文本字符串的子字符串。
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);}}
因為您為“測試1”兩次調用instance.isSubstring(T,P)
-一次保存result
變量,一次在System.out
調用中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.