错误:不兼容的类型?我还能怎么做?
public static int linearSearch(int[] data, int numToFind)
{
int found=-1;
for (int x=0;x<data.length;x++){
**if(data[x] =numToFind)**
found = x ;
break;
}
return found;
}
在那里我有我的错误它说,它在if(data[x]=numToFind)
错误:不兼容的类型?我还能怎么做?
IF在Java语句要求布尔不兼容的类型。你的代码应该是
if(data[x] ==numToFind)
这可能是一个错字。
A mistake users from C background frequently make is confusing the assignment and equality operators = and ==. If you want to compare two values, you should use a double equal sign since that's how Java works. If you accidentally use a single equal sign, you don't compare values but assign a value to a variable. Your code would have perfectly worked in C, but it won't work in Java
里面if
条件应该是boolean
值。你犯了一个错字,应该是:
if (data[x] == numToFind)
你需要把if(data[x] == numToFind)
。
用一只=它是分配。有两个是布尔型 * 运营商 *。
public static int linearSearch(int data[], int numToFind)
{
int found=-1;
for (int x=0;x<data.length;x++){
if(data[x] == numToFind)
found = x ;
break;
}
return found;
}
试试这个。
Heheh,但没有。问题是他用来比较(=和不== ==) – diazazar
解决方案只是==的操作符,不管不抛出错误都不能解决(wrt data []和更早的答案)!!! –
使用''==进行比较,而不是'='... – Doorknob
无关,但要确保所有的'发现= X;'和'打破;'是一组'{}'括号内。正如所写,这个循环将只运行一次。无论数组中的第一个数字是否匹配,您都会“中断”,并且结束循环。 – VoteyDisciple