简体   繁体   中英

An array of integers

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;
}

Boolean method returns true if there are at least the given quantity of the given number in the structure.

You need to first count the times the element occurs in your array. Using a for-each loop , that might look something like

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

or

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

If you want to search for specific element in an array:

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

Your code returns true if the first argument of the have method is >= 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM