-2
我正在編寫一個程序,該程序可以識別字符串「xyz」是否在輸入字符串中進行了規範化處理。我創建了一個變量,用for循環存儲「xyz」的位置,然後將它與前後的字符數進行比較,用.substring()和.length()創建整數。奇怪的是,代碼不會在第一次if後返回true或false,並且不能在後面返回語句。 任何人都可以幫我把這個包裹起來嗎?Java的怪異無法訪問的代碼錯誤
非常感謝!
也許是因爲長度變量尚未運行,對於編譯器來說,它們將始終不同?如何解決這個問題?
public static boolean xyzCenter(String str){
//identifies the position of "xyz" within the String.
int xyzPosition=1;
//loops through the string to save the position of the fragment in a variable.
for(int i = 0; i<str.length(); ++i){
if(str.length()>i+2 && str.substring(i, i+3).equals("xyz")){
xyzPosition=i;
}
}
//ints that determine the length of what comes before "xyz", and the
length of what comes after.
int lengthBeg = str.substring(0, xyzPosition).length();
int lengthEnd = str.substring(xyzPosition+3, str.length()).length();
if ((lengthBeg != lengthEnd));{
return false;
} //this compiles.
return true; //this doesn't!
如果你能解釋一下爲什麼我會投×最大 –
@KickButtowski完成 –
哎呀,這是正確的!謝謝一堆! –