Created
July 31, 2012 13:23
-
-
Save Centaur/3217012 to your computer and use it in GitHub Desktop.
guess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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