如何从用户输入中获取2个数字并在控制台中显示总和?使用Swift

问题描述 投票:0回答:1
    import Foundation

    print("Type your age: ")

    if let input = readLine() {
        print("You typed \(input)\n")
    }

**我以前用过,但是我不知道如何添加它们并将其显示在控制台中。

swift xcode input display addition
1个回答
0
投票
import Foundation

print("enter number one: ")
var sum = 0
if let num1 = Int(readLine()!) {
  sum += num1
}
print("enter number two: ")
if let num2 = Int(readLine()!) {
  sum += num2
}

print(sum)
© www.soinside.com 2019 - 2024. All rights reserved.