重复提示直至输入正确
我最近正在学习 Scala。我以前已经习惯了C和Java。我想知道是否有一种更优雅的方式来重复要求输入,直到给出正确的输入。
val choiceType = {
var in = ""
var pass = false
do {
in = readLine()
pass = in match {
case "1" => println("Not implemented"); true
case "2" => println("Not implemented"); true
case "3" => println("Not implemented"); true
case "4" => println("Not implemented"); true
case "5" => println("Thanks for using."); true
case _ => println("Error input. Please enter again. (Possible value: 1 - 5)"); false
}
} while (!pass)
in.toInt
}
if (choiceType == 5) System.exit(0)
我想知道在 Scala 中是否有更好的方法来做到这一点?
I am picking up Scala recently. I have been used to C and Java before. I am wondering if there is a more elegant way of asking for input repeatedly until correct input is given.
val choiceType = {
var in = ""
var pass = false
do {
in = readLine()
pass = in match {
case "1" => println("Not implemented"); true
case "2" => println("Not implemented"); true
case "3" => println("Not implemented"); true
case "4" => println("Not implemented"); true
case "5" => println("Thanks for using."); true
case _ => println("Error input. Please enter again. (Possible value: 1 - 5)"); false
}
} while (!pass)
in.toInt
}
if (choiceType == 5) System.exit(0)
I am wondering if there is a better way of doing this in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Iterate.continually
一遍又一遍地执行相同的操作,直到施加一些停止条件(使用dropWhile
),或者您也可以使用Iterator。 iterate
为您提供上一行,以防您想在错误消息中使用它:其工作方式是从 readLine 开始,然后如果需要另一行,它会根据上一行宣布错误消息行(显然这是错误的)并读取另一行。然后,您使用
collect
块来挑选出正确的输入;错误的输入只会被丢弃而不会被收集。在本例中,由于您想将其转换为整数,所以我只是传递该行。现在,我们只想要一个好的条目,因此我们获取下一个条目并将其转换为 int。您还可以使用递归函数来做类似的事情:
这里的技巧是,如果输入错误,您只需再次调用该函数即可。由于这是函数中发生的最后一件事,Scala 将避免真正的函数调用,而只是再次跳转到开头(尾递归),因此不会溢出堆栈。
You could use either
Iterate.continually
to do the same thing over and over again until you impose some stopping condition (withdropWhile
), or you could useIterator.iterate
to give you the previous line in case you want to use it in your error message:The way this works is by starting with a readLine, and then if it needs another line it announces an error message based on the previous line (obviously that one was wrong) and reads another line. You then use a
collect
block to pick out your correct input; the wrong input just falls through without being collected. In this case, since you want to turn it into an integer, I'm just passing the line through. Now, we only want one good entry, so we get thenext
one and convert it to an int.You could also use a recursive function to do a similar thing:
Here the trick is that in the case where you get wrong input, you just call the function again. Since that's the last thing that happens in the function, Scala will avoid a true function call and just jump to the beginning again (tail-recursion), so you won't overflow the stack.
递归 FTW,恕我直言。不管怎样,我会建议对 Rex Kerr 的递归解决方案进行一些修改。
Recursion FTW, IMHO. Anyway, I'm gonna suggest a little modification upon Rex Kerr's recursive solution.
Scala 允许您将
{ case }
块视为 PartialFunction 特征的实例。 PartialFunction 为您提供了一种测试该函数是否是为特定输入定义的方法。因此,您可以这样重写:如果您想避免使用可变的
input
变量,您还可以使用 Iterator.continually() (将表达式转换为无限迭代器,该迭代器重复计算表达式)。通过使用 Iterator 的
collect
方法,您可以避免为operation
指定名称。Scala allows you to treat
{ case }
blocks as instances of the PartialFunction trait. PartialFunction gives you a way to test whether the function is defined for a particular input. So you could rewrite like this:If you want to avoid having the mutable
input
variable, you can also use Iterator.continually() (turns an expression into an infinite Iterator which repeatedly evaluates the expression).And you can avoid having to give
operation
a name by using Iterator'scollect
method.我会使用递归函数。像这样的东西:
I would use a recursive function. Something like this: