每個人!我是 Stack Overflow 的新成員,就像我是 swift 編程的初學者一樣。我發這篇文章是為了找出以下情況的解決方案:
我正在使用命令列工具在 Swift 上創建一個應用程式來輸入資料。該應用程式基本上用作身份驗證器。例如,如果有人輸入美國作為國家名稱并且年齡是 17 歲,那么程式將回傳一條訊息,如“您不能申請這個職位”。否則,如果國家名稱是 USA 并且年齡等于或大于 18 歲,則回傳的訊息是“您可以轉到下一步”。我已經多次嘗試設定此條件,但它不起作用。我已經知道函式 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() 函式來檢查條件是否為真。如果為真,則輸出訊息將顯示用戶可以繼續下一步,否則將不允許他繼續。幾天以來我一直在互聯網上尋求解釋,但沒有找到任何可能對我有幫助的東西。我希望使用 Stack Overflow 論壇獲得一些有用的答案。
uj5u.com熱心網友回復:
首先,歡迎來到 Stackoverflow
所以我會把你的問題分成更小的部分
第一個,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。到達這里你確定 theageString不為 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()
}
}
uj5u.com熱心網友回復:
解決方案已解決!
嘿,伙計們,首先我要感謝你們所有有用的答案,這對我幫助很大。我終于找到了解決方案,并將與您分享。
我做了什么?我剛剛創建了兩個變數,一個 String 和另一個 Integer。然后,使用 if var 強制展開 Int 變數,我做了一個 if 陳述句來檢查這兩個條件是否為真(在這種情況下,如果此人來自美國并且年齡等于或大于 18 歲) . 現在,程式按照我想要的方式運行。如果您來自美國但未滿 18 歲,輸出將回傳一條訊息,表明您無法申請。否則,您可以轉發。
我會讓上面的代碼。如果您想提出一些意見或任何建議,我們將不勝感激。
再次非常感謝您的所有回答。
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.")
}
}
uj5u.com熱心網友回復:
我希望這對你有幫助 :)
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)")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/536526.html
下一篇:更改時的Xcode未觸發
