2017-10-01 24 views
-5

我不知道如何評價這條線,瞭解我的代碼確實 而(我< data.length & &!結果){困惑於如何評價一會兒!導致,而功能

這是我的完整代碼。

public static boolean {2,6,-3,7,3} (int[] data, int val) { 
    boolean result = false; 
    int i = 0; 

    while (i < data.length && !result) { 
    if (data[i] == val) { 
     result = true; 
    } 
    i++; 
    } 
    return result; 
} 
+2

'public static boolean {2,6,-3,7,3}'在我所知的任何語言中都是無效的。 –

+2

另外...你的問題到底是什麼? –

回答

1

result變量被用作一個方式i到達data.length之前打出來的while循環的。它會導致當result變爲true時退出循環。因此,一旦

int i = 0; 

while (i < data.length) { 
    if (data[i] == val) { 
     return true; // here we break from the loop using a return statement 
        // when a match is found 
    } 
    i++; 
} 

return false; 
1
boolean result = false; //initial declaration 
int i = 0; 

while (i < data.length && !result) { 
    if (data[i] == val) { 
     result = true; 
     // the !result evaluates to false once you've reached this statement 
    } 

,你發現在data[]匹配的val

它等效於:

boolean result = false; 
int i = 0; 

while (i < data.length) { 
    if (data[i] == val) { 
     result = true; 
     break; // here we break from the loop explicitly when result becomes true 
    } 
    i++; 
} 

return result; 

或更簡單。流量爆發的循環,因爲條件變爲

any() && false => false 

其中any()可能是真或根據您的第一個條件i < data.length假。