1

我是一名新程序员,目前正在按照教程进行工作。我一直在尝试让用户输入我的代码,我在网上找到的每个示例似乎都表明这是正确的语法。显然我做错了什么:) Xcode 报告:“使用未解析的标识符'输入'”在此先感谢。

import Foundation

var diceRoll = Int(arc4random_uniform(100) + 1)
print("\(diceRoll)")

print("Enter a number between 1 and 100.")

var userinput=input()
let intinput=userinput.toint()

if intinput == diceRoll {print("correct guess")}
  if intinput < diceRoll {print("too low")}
   else if intinput > diceRoll {print("too high")}
4

1 回答 1

0
import Foundation

var diceRoll = Int(arc4random_uniform(100) + 1)
print("\(diceRoll)")

var i = 0
repeat {
    print("Enter a number between 1 and 100:")

    if let inputString = readLine() {
        if let inputNumber = Int(inputString) {
            i = inputNumber
        }
    }

} while i < 1 || i > 100
/* console output
24
Enter a number between 1 and 100.
0
Enter a number between 1 and 100.
101
Enter a number between 1 and 100.
100
Program ended with exit code: 0
*/

.

func readLine(stripNewline stripNewline: Bool = default) -> String?

返回从标准输入读取到当前行末尾或直到到达 EOF 的字符,如果已经到达 EOF,则返回 nil。

如果 stripNewline 为真,换行符和字符组合将从结果中删除。这是默认设置。

标准输入被解释为 UTF-8。无效字节将被 Unicode 替换字符替换。

于 2015-11-20T12:26:22.837 回答