简体   繁体   English

运算符 ',=' 不能应用于 'int'、'int[]'

[英]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比较 integer 和 integer 数组

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[]).您收到错误消息是因为“costs”是一个二维矩阵,而“coins”是一个 integer。因此,您不能将整数 (int) 与整数数组 (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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