2015-10-26 53 views
1
public static boolean have(int x, int count) { 
    int i; 
    count = 1; 
    for (i = 0; i < array.length; i++) { 
     if (x >= 1) return true; 
    } 
    return false; 
} 

如果结构中至少有给定数量的给定数量,则布尔方法返回true。整数阵列

回答

2

您需要先计算时代的元素

if (arrray[i] == x) { 
    return true; 
} 

您的代码返回true发生在你的数组中。使用for-each loop,这可能看起来像

int vcount = 0; 
for (int val : array) { 
    if (x == val) { 
     vcount++; 
     if (vcount >= count) return true; 
    } 
} 
return false; 

return vcount >= count; // <-- to handle count == 0. 
+0

这样的作品,非常感谢你 – Sara

0

如果你想在数组中搜索特定的元素:如果have方法的第一个参数是>= 1