簡體   English   中英

運算符 && 不能應用於 'boolean', 'int

[英]Operator && cannot be applied to 'boolean', 'int

我正在嘗試覆蓋 equals 方法,但面臨“Operator && cannot be applied to 'boolean','int'' 錯誤,這似乎是由最后一行引起的。 我的信念是pages == otherEntry.pages返回一個 int,但為什么呢?


        @Override
         public boolean equals (Object that) {
            if (this == that) return true;
            if (!(that instanceof BookEntry)) return false;

            BookEntry otherEntry = (BookEntry) that;

            return title.equals(otherEntry.title) &&
                    Arrays.equals(authors, otherEntry.authors) &&
                    Float.compare(otherEntry.rating, rating) &&
                    ISBN.equals(otherEntry.ISBN) &&
                    pages == otherEntry.pages;
        }

Float.compare(otherEntry.rating, rating)返回一個int

如果要根據Float.compare()定義的順序確定兩個浮點數是否相等,請將其更改為Float.compare(otherEntry.rating, rating) == 0

return 語句將變成

        return title.equals(otherEntry.title) &&
                Arrays.equals(authors, otherEntry.authors) &&
                Float.compare(otherEntry.rating, rating) == 0 &&
                ISBN.equals(otherEntry.ISBN) &&
                pages == otherEntry.pages;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM