我是Swift的新手,我正在努力建立一个简单的程序,告诉用户他们出生在中国日历的年份根据他们的年龄。
var string1 = "You are year of the"
let age:Int? = Int(ageField.text!)
if age <= 12 {
let remainder = age!
} else {
let remainder = age! % 12
}
if remainder == 0 {
string1 += " sheep."
}; if remainder == 1 {
string1 += " horse."
}; if remainder == 2 {
string1 += " snake."
}; if remainder == 3 { // And so on and so forth...
我在每条“if”行上收到一条错误消息,指出二元运算符'=='不能应用于'_'和'Int'类型的操作数。我有什么想法可以解决这个问题?
答案 0 :(得分:2)
变量/常量SELECT Sub.name, Sub.N_Certificates,
MAX(Sub.N_Certificates) OVER ()
-
Sub.Certificates AS Difference
FROM ($_query_$) AS SUB
应该在remainder
构造之外声明,你也可以删除字符&#34 ;;&#34;在你的代码中。斯威夫特不需要&#34;;&#34;在指令结束时,如objective-c
答案 1 :(得分:2)
作为Alessandro答案的摘要和优化代码看起来像
的评论var string1 = "You are year of the"
if let age = Int(ageField.text!) {
let remainder = age % 12
if remainder == 0 {
string1 += " sheep."
} else if remainder == 1 {
string1 += " horse."
} else if remainder == 2 {
string1 += " snake."
} // And so on and so forth...
} else {
print("please enter a number")
}
或使用switch
语句
var string1 = "You are year of the "
if let age = Int(ageField.text!) {
switch age % 12 {
case 0: string1 += "sheep."
case 1: string1 += "horse."
case 2: string1 += "snake."
// And so on and so forth...
}
} else {
print("please enter a number")
}
PS:实际上羊是山羊; - )