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

比較 integer 和 integer 數組

您收到錯誤消息是因為“costs”是一個二維矩陣,而“coins”是一個 integer。因此,您不能將整數 (int) 與整數數組 (int[]) 進行比較。 嘗試在“成本”上循環兩次以與所有值進行比較

    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