如何将readLine()的Swift 3输出转换为Integer? [英] How to convert Swift 3 output of readLine() to Integer?

查看:88
本文介绍了如何将readLine()的Swift 3输出转换为Integer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您阅读全文之前,请勿将其标记为重复项。这是特定于Swift 3的。



我有一些函数,这些函数具有Ints,Floats等参数。我想获取readLine()的输出并具有Swift接受readLine()的输出作为这些类型,但是不幸的是readLine()输出String?当我尝试转换时,它告诉我它没有展开。我需要帮助。我正在使用Ubuntu 16.04。



例如,如果我有 area(宽度:15,高度:15),如何将15和15替换为两个包含readLine()或与readLine()等效的常量以接受终端用户的输入?



我正在编写的程序专门用于数学运算,因为大多数人似乎对字符串感到满意,所以这实际上是一个基于CLI的计算器。



编辑1 (笑)好的,这是上面的更确切的解释。以下代码将打印梯形的区域:

  import Foundation 

func areaTrapezoid(height: Float,baseOne:Float,baseTwo:Float){
let inside = baseOne + baseTwo
let outside = 0.5 * height
let result = outside * inside
print( Area of梯形是\(结果))
}

areaTrapezoid(高度:10,baseOne:2,baseTwo:3)
  import Foundation 

func areaTrapezoid(height:Float, baseOne:浮点数,baseTwo:浮点数){
let inside = baseOne + baseTwo
let outside = 0.5 * height
let result = outside * inside
print(梯形面积为\(结果))
}

let h = readLine()
areaTrapezoid(height:h,baseOne:2,baseTwo:3)

除了已经显而易见的是,readLine()将输出一个可选字符串,而不是Float。我希望用户能够以交互方式通过CLI输入数字。我只是在学习Swift,但是当我学习该语言时,我在C ++中做了类似的事情。谢谢您可以提供的任何帮助。

解决方案

readLine()返回



要解包字符串,可以使用 if let 并将字符串转换为整数,请使用 Int()



示例:

 导入Foundation 
如果let输入= readLine(){
如果let num = Int(输入){
print(num)
}
}

假设您两次提示用户:

 让提示符1 = readLine()
让提示符2 = readLine()

然后:

 如果让response1 =提示1,
response2 =提示2,
num1 = Int(response1),
num2 = Int(response2){
print( \(num1)和\(num2)之和为\(num1 + num2))
}


Please don't mark as duplicate until you read the whole thing. This is specific to Swift 3.

I have functions that have parameters such as Ints, Floats, etc. I'd like to take the output of readLine() and have Swift accept the output of readLine() as these types, but unfortunately readLine() outputs a String? and when I try to convert it tells me it's not unwrapped. I need help. I'm using Ubuntu 16.04.

For example, if I had area(width: 15, height: 15), how would I replace 15 and 15 with two constants containing readLine() or any equivalent to readLine() to accept input from a user in the terminal?

Also note that the program I am writing is specifically doing math, as most people seem to be happy with strings, this is literally a CLI-based calculator.

EDIT 1 (lol) Okay, here's a more exact explanation of above. The following code will print the area of a trapezoid:

import Foundation

func areaTrapezoid(height: Float, baseOne: Float, baseTwo: Float) {
    let inside = baseOne + baseTwo
    let outside = 0.5 * height
      let result = outside * inside
        print("Area of Trapezoid is \(result)")
  }

areaTrapezoid(height: 10, baseOne: 2, baseTwo: 3)

So, the trapezoid has a height of 10 units, and two bases that have lengths of 2 and 3 respectively. However, I want to do something like:

import Foundation

func areaTrapezoid(height: Float, baseOne: Float, baseTwo: Float) {
    let inside = baseOne + baseTwo
    let outside = 0.5 * height
      let result = outside * inside
        print("Area of Trapezoid is \(result)")
  }

let h = readLine()
areaTrapezoid(height: h, baseOne: 2, baseTwo: 3)

Except, as is already obvious, readLine() will output an optional string, and not a Float. I want the user to be able to input the numbers via CLI in sort of an interactive way, if you will. I'm just learning Swift, but I did something similar in C++ when I was learning that language. Thanks for any help you can provide.

解决方案

readLine() returns an Optional String.

To unwrap the String, you can use if let, and to convert the String to an integer, use Int().

Example:

import Foundation
if let typed = readLine() {
  if let num = Int(typed) {
      print(num)
  }
}

Let's say you prompted the user twice:

let prompt1 = readLine()
let prompt2 = readLine()

Then:

if let response1 = prompt1, 
    response2 = prompt2, 
    num1 = Int(response1), 
    num2 = Int(response2) {
    print("The sum of \(num1) and \(num2) is \(num1 + num2)")
}

这篇关于如何将readLine()的Swift 3输出转换为Integer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆