Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created July 31, 2012 13:23
Show Gist options
  • Save Centaur/3217012 to your computer and use it in GitHub Desktop.
Save Centaur/3217012 to your computer and use it in GitHub Desktop.
guess
val target = util.Random.nextInt(100)
val scanner = new java.util.Scanner(System.in)
var times = 0
def askAndCompare {
print("Guess a number: ")
val input = scanner.nextInt
times += 1
if(input < target){
println("Too small! Try again.")
askAndCompare
} else if(input > target) {
println("Too big! Try again.")
askAndCompare
} else {
println("Bingo! You get the right answer in %d tries!".format(times))
}
}
askAndCompare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment