๐ฐInput/Output
๐พ ็จๅผ๏ผreplit โฉ readLine()
// ่ซไฝฟ็จ่
่ผธๅ
ฅ่ณๆ
// ้ ่จญๅ้ก๏ผใ่ซ่ผธๅ
ฅไธ็ตๆธๅญ๏ผใ
func getInput(from question: String = "่ซ่ผธๅ
ฅไธ็ตๆธๅญ๏ผ ") {
// 1. ๅ
้กฏ็คบๅ้ก
print(question, terminator: "")
// 2. ็ขบๅฎ่ผธๅ
ฅๆฏๆดๆธ
guard
let input = readLine(), // 2.1 ๅ
็ขบๅฎๆ่ผธๅ
ฅ
let n = Int(input) // 2.2 ๅจ็ขบๅฎ่ผธๅ
ฅ็ๆฏๆดๆธ
else {
// 2.3 ๅฆๆ่ผธๅ
ฅไธๆญฃ็ขบ๏ผๅฐฑ้กฏ็คบ่ญฆ่ช
print("ๆ่จโ๏ธ่ซ่ผธๅ
ฅๆดๆธๅฅฝๅ ๐")
// 2.4 ็ถๅพๅๅไธๆฌก
return getInput(from: question)
// โญ ๆณจๆ๏ผ`return` ๆฏๅฟ
่ฆ็๏ผๅฆๅๆ็ข็๏ผ
// โ error: 'guard' body must not fall through
}
// 3. ็จๅผๅฆๆๅฐ้่ฃก๏ผๅฐฑ่กจ็คบ่ผธๅ
ฅ็ๆฏๆดๆธ๏ผ่ไธๅฏไปฅ็จ n ่กจ็คบ
print("ๆจ่ผธๅ
ฅ็ๆธๅญๆฏ \(n)")
}
// ๅท่ก็จๅผ
getInput()
Swift โฉ Standard Library โฉ Input and Output
Last updated