我有一个方法接受输入数字并将其与整数数组中的数字进行比较,并根据找到的值返回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