我有一個方法接受輸入數字並將其與整數數組中的數字進行比較,並根據找到的值返回true或false。該方法工作正常,但我無法解釋其中的一行代碼。具體在while
聲明(!found && index < accounts.length)
。我的大腦解釋這個的方式是「雖然發現不是虛假的,並且索引小於帳戶數組的長度」......但我不明白!found
部分可以如何與<
運算符進行比較。完整的方法如下。Java:不明白'條件'
public static boolean account(int input) {
// account array
int accounts[] = { 5658845, 4520125, 7895122, 8777541, 4581002 };
// Loop control variable
int index = 0;
// Search results
boolean found = false;
// Search the array.
while (!found && index < accounts.length) {
if (accounts[index] == input) {
found = true;
}
// Increment index
index++;
}
// Return result of search
return found;
}
您能儘快返回TRUE;因爲你已經匹配了元素。 'found'變量甚至不需要。 –
@ theProgrammer101從技術上講'while'是關鍵字和聲明。 – EJP
@EJP你會在條件語句中調用嗎? – CodingNinja