swift - Random Number Generator (Command Line) -


when user enters number have guess random number. these errors getting line of code in code twice.

"value of type "string has no number "stringbyreplacingoccruencesofstring"

input = input.stringbyreplacingoccurrencesofstring("\n", withsring:"", 

here full code

import foundation  var randomnumber = 1 var userguess:int? = 1 var continueguessing = true var keepplaying = true var input = ""  while (keepplaying) { randomnumber = int(arc4random_uniform(101))  // gets random number between 0-   1000  print("the random number guess is:\(randomnumber)"); while (continueguessing) {     print ("print number between 0 , 100")     input = nsstring(data: filehandle.standardinput.availabledata,     encoding:string.encoding.utf8.rawvalue) as! string // gets keyboard input     input = input.stringbyreplacingoccurrencesofstring("\n", withsring:"",     options: nsstring.compareoptions.literalsearch, range: nil)     userguess = int(input)     if (userguess == randomnumber){         continueguessing = false         print("correct number!");     }     else if (userguess! > randomnumber){         print("your guess high");     }     else{         print("your guess low");      } } print ("play again? y or n"); input = nsstring(data: filehandle.standardinput.availabledata, encoding:string.encoding.utf8.rawvalue)! string input = input.stringbyreplacingoccurrencesofstring("\n", withstring: "", options: nsstring.compareoptions.literalsearch, range: nil)  if(input == "n" || input == "n"){     keepplaying = false } continueguessing = true  } 

fixed code

import foundation  var randomnumber = 1 var userguess:int? = 1 var continueguessing = true var keepplaying = true var input = ""  while (keepplaying) { randomnumber = int(arc4random_uniform(101)) // gets random number between 0-   1000  print("the random number guess is:\(randomnumber)"); while (continueguessing) {     print ("print number between 0 , 100")     if let input = readline() {         if let userguess = int(input) {             if (userguess == randomnumber){                 continueguessing = false                 print("correct number!");             }             else if (userguess > randomnumber) {                 print("your guess high");             }             else{                 print("your guess low");              }          } else {             print("bad input data");         }     } } print ("play again? y or n"); input = readline()! if(input == "n" || input == "n"){     keepplaying = false } continueguessing = true  } 

result

enter image description here


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -