[英]Swift - Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
Not really understanding why this isn't working. 不太了解为什么这行不通。 I'm pretty new to the Swift world.
我是Swift世界的新手。
The error I'm getting is Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
我得到的错误是
Binary operator '>=' cannot be applied to operands of type 'String' and 'Int'
Could anyone help me understand why I'm getting this error? 谁能帮助我了解为什么我会收到此错误? Do I need to convert the String to a Double or is there something else I'm totally missing?
我需要将String转换为Double还是我完全缺少的其他东西? Again I'm new to Swift.
我还是Swift的新手。
Do I need to convert the String to a Double?
我需要将字符串转换为Double吗?
Yes, that's basically it. 是的,基本上就是这样。
You must declare first a variable to accumulate all the inputs: 您必须首先声明一个变量以累加所有输入:
var inputs = [Double]()
Observe that I'm declaring an array of Double
because that's what we are interested in. 观察到我在声明一个
Double
数组,因为这是我们感兴趣的。
Then, each time you ask the input, convert the obtained String
to Double
and store it in your array: 然后,每次您询问输入时,都将获取的
String
转换为Double
并将其存储在数组中:
print("Please enter a temperature\t", terminator: "")
var message : String = readLine()!
let value : Double = Double(message)!
inputs.append(value)
Finally, check all the accumulated values in inputs
(you got this part right): 最后,检查
inputs
所有累加值(正确理解此部分):
for value in inputs {
// value is already a Double
if value >= 80 {
message = "hot!"
}
// etc.
}
I suggest researching how to convert to Double
with error checking (ie how to detect "100 hot!" and ignore it because can't be converted). 我建议研究如何通过错误检查转换为
Double
(即,如何检测“ 100 hot!”并由于无法转换而将其忽略)。
Also, consider using a loop to read the values. 另外,考虑使用循环读取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.