簡體   English   中英

如何使用 Swift 上的 readLine() 比較兩種不同類型的數據(String 和 Int)?

[英]How can I compare two different types of data (String and Int) using readLine() on Swift?

大家好,我是 Stack Overflow 的新成員。 就像我是 swift 編程的初學者一樣:我寫這篇文章是為了找出以下情況的解決方案:

我正在 Swift 創建一個使用命令行工具輸入數據的應用程序。 該應用程序基本上用作身份驗證器。 例如,如果有人輸入美國作為國家名稱並且年齡是 17 歲,那么程序將返回一條消息,如“您不能申請這個職位”。 否則,如果國家名稱是 USA 並且年齡等於或大於 18 歲,則返回的消息是“您可以轉到下一步”。 我已經多次嘗試設置此條件,但它不起作用。 我已經知道 function readLine() 是一個可選字符串,但是我怎樣才能讓我的程序正常工作呢? 它遵循我上面的代碼讓你理解我的想法。

我真的很感激任何幫助。 同樣,我是初學者,我已經在學習 Swift 種語言,但我正在尋找一些處理整數和字符串並比較這兩種數據類型的解決方案。 非常感謝你!

我的代碼是:

import Foundation

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var country, var age = readLine(){
    if country == "USA" && age < "18" {
        print("You're not allowed to apply to this position.")
    } else {
        print("You can forward to the next step.")
    }
    
}


PS:如您所見,我錯誤地將變量age用作String,但我想將其轉換為Int類型,然后檢查國家名稱是否與我分配的值相同或年齡是否相等或高於18。但到目前為止還沒有找到解決方案。

我正在嘗試找到一種解決方案來比較 Swift 上的兩種不同類型,使用命令行工具和 readLine() function 來檢查條件是否為真。 如果為真,將顯示 output 消息,提示用戶可以進行下一步,否則不允許跟隨。 幾天以來我一直在 inte.net 上尋找解釋,但沒有找到任何可能對我有幫助的東西。 我希望使用 Stack Overflow 論壇獲得一些有用的答案。

第一個, readline()意味着你讀到當前行的末尾。 作為您檢查條件時的代碼,您再次調用readLine() 錯誤的部分在這里。

我建議您先閱讀,然后再執行所有邏輯。 一開始你只需要閱讀一次

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

接下來,檢查它是否為 nil(因為 value 的選項值可以為 nil)

if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

然后檢查是否可以轉換為 Int。 到達這里你確定ageString不為 nil 因為你已經在上面檢查過了。 所以你只需簡單地轉換

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

然后畢竟,你只是檢查你的條件

完整的代碼將是這樣的

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

if country == "USA" && age < 18 {
    print("You're not allowed to apply to this position.")
} else {
    print("You can forward to the next step.")
}

其他方法是使用if let來實現,所以沒有強制解包

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

if let ageString = ageString {
    if let age = Int(ageString) {
        if country == "USA" && age < 18 {
            print("You're not allowed to apply to this position.")
        } else {
            print("You can forward to the next step.")
        }
    } else {
        print("Error age not a number")
        fatalError()
    }
}

解決方案已解決!

嘿,伙計們,首先我要感謝你們所有有用的答案,這對我幫助很大。 我終於找到了解決方案,並將與您分享。

我做了什么? 我剛剛創建了兩個變量,一個 String 和另一個 Integer。然后,使用 if var 強制展開 Int 變量,我做了一個 if 語句來檢查兩個條件是否為真(在這種情況下,如果這個人是來自美國且年齡等於或大於 18 歲)。 現在,程序按照我想要的方式運行。 如果您來自美國但沒有滿 18 歲,output 將返回一條消息,您無法申請。 否則,您可以轉發。

我會讓上面的代碼。 如果您想提出一些意見或任何建議,我們將不勝感激。

再次非常感謝您的所有回答。

var countryCheck = "USA"
var ageCheck: Int = 18

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var countryCheck = country, var ageCheck = Int(age!) {
    if countryCheck == "USA" && ageCheck >= 18 {
        print("You can apply.")
    } else {
        print("You can not apply to this position.")
    }
}

我希望這對你有幫助:)

import Foundation

print("Enter your country: ")
if let country = readLine() {
  if let num = Int(country) {
      print(num)
      }
    }
      let country = readLine()
let age = readLine()


if let USA = country, 
    let num1 = Int(USA), 
    let num2 = Int(USA) {
    print("The age of \(num1) and \(num2) is \(num1 + num2)")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM