使用Swift的错误很少

时间:2015-03-17 02:25:34

标签: swift

我试图使用Xcode中的命令行工具构建一个小型随机数猜测游戏。

我还添加了来自Xcode ide

的错误图像
import Foundation

var randomNumber = 1
var userGuess = 1
var continueGuessing = true
var keepPlaying = true
var input = ""

while (keepPlaying) {
    randomNumber = Int(arc4random_uniform(101)) //to get a random number between 0-100
    println(" The random number to guess is: \(randomNumber)"  );
    while (continueGuessing) {
        println(" Pick a number between 0 and 100. ")
        input = NSString(data: NSFileHandle.fileHandleWithStandardInput().
            availableData,encoding:NSUTF8StringEncoding)! //get keyboard input
        input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options:NSStringCompareOptions.LiteralSearch, range: nil)//strip off the/n
        userGuess = input.toInt()!
        if (userGuess == randomNumber) {
            continueGuessing = false
            println("correct number!");

        }
//nested if statement
        else if (userGuess > randomNumber){
            //user guessed to high
            println("your guess is too high");
        }

    else {
        //no reason to check if userGuess < random. it has to be.
        println("Play again? Y or N");
        input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData,
            encoding:NSUTF8StringEncoding!
            input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: <#NSStringCompareOptions#>.LiteralSearch, range: nil);
            }  if (input == "N" || input == "n"){
            keepPlaying = false
        }
        continueGuessing = true

Here is an image of the code from Xcode ide

1 个答案:

答案 0 :(得分:1)

你有两个小错误:

你在这里有一个换行符,还需要强制转换为String - 改变它:

input = NSString(data: NSFileHandle.fileHandleWithStandardInput().
        availableData,encoding:NSUTF8StringEncoding)!

到此:

input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)! as String

您还为NSStringCompareOptions留下了XCode自动生成的占位符 - 更改此内容:

input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: <#NSStringCompareOptions#>.LiteralSearch, range: nil);

到此:

input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

以下是上面更正的完整代码段:

while (keepPlaying) {
randomNumber = Int(arc4random_uniform(101)) //to get a random number between 0-100
println(" The random number to guess is: \(randomNumber)"  );
while (continueGuessing) {
    println(" Pick a number between 0 and 100. ")
    input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)! as String
    input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options:NSStringCompareOptions.LiteralSearch, range: nil)//strip off the/n
    userGuess = input.toInt()!
    if (userGuess == randomNumber) {
        continueGuessing = false
        println("correct number!");

    }
        //nested if statement
    else if (userGuess > randomNumber){
        //user guessed to high
        println("your guess is too high");
    }

    else {
        //no reason to check if userGuess < random. it has to be.
        println("Play again? Y or N");
        input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)! as String
            input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil);
    }  if (input == "N" || input == "n"){
        keepPlaying = false
    }
    continueGuessing = true