简体   繁体   中英

Operator '!=' cannot be applied to 'int', 'int[]'

public static int maxIceCream(int[][] costs, int coins) {
    Arrays.sort(costs);
    boolean found = false;

    for (int i = 0; i < costs.length; ++i) {
        if (coins != costs[i]) {
            coins -= costs[i];
            found = true;
            break;
        } else {
            return i;
        }
    }
    return costs.length;
}

compare to integer and integer array

You are getting the error message because "costs" is a 2D matrix and "coins" is an integer. So, you can't compare an integer(int) to array of integer(int[]). Try looping two times over the "costs" to compare to all the values

    public static int maxIceCream(int[][] costs, int coins) {
    Arrays.sort(costs);
    boolean found = false;

    for (int i = 0; i < costs.length; ++i) {
        for (int j = 0; j < costs[i].length; ++j) {
            if (coins != costs[i][j]) {
                coins -= costs[i][j];
                found = true;
                break;
            } else {
                return i;
            }
        }

    }
    return costs.length;
}

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