我是 Swift 新手,不明白为什么会出现以下错误:
exit status 1
main.swift:17:13: error: cannot convert value of type 'Int' to expected argument type 'Bool'
else if remainder2 && remainder == 0{
^~~~~~~~~~
main.swift:21:12: error: cannot convert value of type 'Int' to expected argument type 'Bool'
else if remainder && remainder3 == 0 {
^~~~~~~~~
对于以下代码:
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
let remainder = year % 4
let remainder2 = year % 400
let remainder3 = year % 100
var whatLeap = false
if remainder == 0 {
whatLeap = true
} else if remainder2 && remainder == 0 {
whatLeap = true
} else if remainder && remainder3 == 0 {
whatLeap = false
} else {
whatLeap = false
}
if whatLeap == true {
print("YES")
} else {
print("NO")
}
}
isLeap(year: aYear)
我尝试将变量“whatLeap”更改为字符串,但出现类似的错误。
任何帮助将不胜感激!
最佳答案
您需要完整地陈述这两个陈述:
remainder2 == 0 && remainder == 0
如果您按原样保留该语句,您所说的是remainder2 == true &&remaining == 0
,这可以解释为什么您会收到错误消息Int to Bool。
再举一个例子:
var tOrF = false
if tOrF == true {}
和 if tOrF {}
是相同的语句。
关于swift - 无法将类型 'Int' 的值转换为预期参数类型 'Bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59416021/