gpt4 book ai didi

scala - (按任意键继续)在 Scala 中

转载 作者:行者123 更新时间:2023-12-03 00:24:21 27 4
gpt4 key购买 nike

我是一个 scala 新手,有个问题。

我想从文件中读取文本并一次返回三行,然后等待(任何)键盘输入。问题是让程序在继续之前等待输入。 For 循环等显然会忽略 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

最佳答案

你可以这样做:

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 到 9)

关于scala - (按任意键继续)在 Scala 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com