mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Switch to readln
readln throws an exception when a file hits EOF, wheras readLine returns null. That puts us in an infinite loop if we hit EOF.
This commit is contained in:
@@ -37,14 +37,14 @@ fun main() {
|
|||||||
// an answer or a blank string
|
// an answer or a blank string
|
||||||
fun ask(question: String): String {
|
fun ask(question: String): String {
|
||||||
print("$question? ")
|
print("$question? ")
|
||||||
return readLine()?.uppercase() ?: ""
|
return readln().uppercase() ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special case for a "yes or no" question, returns true of yes
|
// Special case for a "yes or no" question, returns true of yes
|
||||||
fun askYesOrNo(question: String): Boolean {
|
fun askYesOrNo(question: String): Boolean {
|
||||||
return generateSequence {
|
return generateSequence {
|
||||||
print("$question? ")
|
print("$question? ")
|
||||||
readLine()
|
readln()
|
||||||
}.firstNotNullOf { yesOrNo(it) }
|
}.firstNotNullOf { yesOrNo(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user