Few Errors Using Swift -
i trying build small random number guessing game using command line tool in xcode.
i have added image of errors come 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 random number between 0-100 println(" random number guess is: \(randomnumber)" ); while (continueguessing) { println(" pick number between 0 , 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 high println("your guess high"); } else { //no reason check if userguess < random. has 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
you've got 2 small mistakes:
you have newline here , need cast string - change this:
input = nsstring(data: nsfilehandle.filehandlewithstandardinput(). availabledata,encoding:nsutf8stringencoding)!
to this:
input = nsstring(data: nsfilehandle.filehandlewithstandardinput().availabledata, encoding: nsutf8stringencoding)! string
you left xcode autogenerated placeholder nsstringcompareoptions - change this:
input = input.stringbyreplacingoccurrencesofstring("\n", withstring: "", options: <#nsstringcompareoptions#>.literalsearch, range: nil);
to this:
input = input.stringbyreplacingoccurrencesofstring("\n", withstring: "", options: nsstringcompareoptions.literalsearch, range: nil)
here full snippet corrections above:
while (keepplaying) { randomnumber = int(arc4random_uniform(101)) //to random number between 0-100 println(" random number guess is: \(randomnumber)" ); while (continueguessing) { println(" pick number between 0 , 100. ") input = nsstring(data: nsfilehandle.filehandlewithstandardinput().availabledata, encoding: nsutf8stringencoding)! 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 high println("your guess high"); } else { //no reason check if userguess < random. has be. println("play again? y or n"); input = nsstring(data: nsfilehandle.filehandlewithstandardinput().availabledata, encoding: nsutf8stringencoding)! string input = input.stringbyreplacingoccurrencesofstring("\n", withstring: "", options: nsstringcompareoptions.literalsearch, range: nil); } if (input == "n" || input == "n"){ keepplaying = false } continueguessing = true
Comments
Post a Comment