java 运算符“!=”不能应用于“int”、“int[]”

rjee0c15  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(436)
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;
}

与整数和整数数组比较

72qzrwbm

72qzrwbm1#

您收到错误消息是因为“costs”是一个2D矩阵,而“coins”是一个整数。因此,您无法比较整数(int)和整数数组(int[])。请尝试在“costs”上循环两次,以比较所有值

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

相关问题