(按任意键继续)在 Scala 中
Posted
技术标签:
【中文标题】(按任意键继续)在 Scala 中【英文标题】:(Press any key to continue) in Scala 【发布时间】:2015-01-23 20:57:45 【问题描述】:我是一个有问题的 Scala 新手。
我想从文件中读取文本并一次返回三行,然后等待(任何)键盘输入。问题是让程序在继续之前等待输入。 For-loops 等显然忽略了 readLine():s.
谢谢
val text = source.fromFile(file.txt).getLines.toList
var line = 0
while (line <= text.size)
readLine(text(line) + "\n" + <Press any key to continue>)
line += 1
【问题讨论】:
请发布您的代码。 【参考方案1】:你可以这样做:
def getNext3From[T](list : Seq[T]) =
val (three, rest) = list splitAt 3 //splits into two lists at the 3 index. Outputs a tuple of type (Seq[T],Seq[T])
println(three) //calls tostring on the list of three items
println("Press any key to continue")
readChar() //waits for any key to be pressed
rest //returns the remainder of the list
@scala.annotation.tailrec //Makes sure that this is a tail recursive method
//Recursive method that keeps requesting the next 3 items and forwarding the new list on until empty
def recursiveLooper[T](list : Seq[T]) : Seq[T] =
list match
case Nil => List()
case rlist => recursiveLooper(getNext3From(rlist))
样本 -> recursiveLooper(1 to 9)
【讨论】:
哦哇...乍一看我什至不认识一半的命令...但是我会解决的!谢谢! 我会评论它,如果你觉得它适合你,那么不要忘记接受作为答案。以上是关于(按任意键继续)在 Scala 中的主要内容,如果未能解决你的问题,请参考以下文章
powershell 如何在PowerShell中编写“按任意键继续”