我正在尝试编写一个方法,该方法接受一个数字月份的int参数,并返回给定月份中的天数。因此,参数1将返回31,因为1月份有31天,参数2将返回28,因为2月份有28天。
这是我到目前为止所拥有的:
Alamofire.request(.POST, "API_URL, parameters).responseJSON{ request, response, result in
print(result.value)
}
我不断收到错误消息" operator ||不能应用于boolean,int。"任何人都可以帮我弄清楚该怎么做?
答案 0 :(得分:2)
你想要这样的东西:
if ( (month == 1) || (month == 3) || (month == 5) || (month == 7)
|| (month == 8) || (month == 10)
答案 1 :(得分:2)
您可以使用java.time.Month
避免使用新Java 1.8时间API的任何开关或if / else语句:
class Category < ActiveRecord::Base
has_one :category_collection
has_one :collection, through: :category_collection
end
class Collection < ActiveRecord::Base
has_many :category_collections
has_many :categories, through: :category_collections
end
class CategoryCollection < ActiveRecord::Base
self.table_name = 'categories_collections'
belongs_to :category
belongs_to :collection
validate :only_one_category
def only_one_category
if CategoryCollection.all.pluck(:category_id).include?(category.id)
errors.add(:category, "can only have one collection")
end
end
end
答案 2 :(得分:0)
||
将导致boolean
,然后boolean
|| int
无效操作
你需要的是
month == 1 || month == 3 || ...
或Map<Integer, Integer> monthNumberToMaxDaysMap
也是这种方式你没有覆盖闰年的情景,它适合重用已经解决的问题
public static void daysInMonth(int month) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, month -1);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH)
}
上述方法仅考虑当前年份,您可以利用它来传递年份
答案 3 :(得分:0)
您可以使用||布尔表达式上的运算符:
if (month == 1 || month == 3 || month == 5) {
// do something ...
}