在scala中阅读多行作为输入

jtjikinw  于 12个月前  发布在  Scala
关注(0)|答案(2)|浏览(240)

我有很多行作为输入,想读取和打印完全相同的东西。问题是我不知道怎么做!(readLine)输入来自用户在终端输入。
第一个整数是总行数!
输入:

4
word
localization
internationalization

字符串
产出:

word
localization
internationalization

wsxa1bj1

wsxa1bj11#

// default condition: string is not empty, not inclusive! 
class TermLines (cond: (String => Boolean) = _ != "") extends Iterator[String] { 
  var s = readLine; 
  def hasNext = cond(s); 
  def next = { var r = s; s = readLine; r } 
}

字符串
那就叫它

println((new TermLines).toVector.mkString("\n")) // till empty


println(new TermLines(_ != ".").toVector.mkString("\n")) // till last is dot

mwkjh3gx

mwkjh3gx2#

解决了!

import scala.io.StdIn

object Solution extends App
{
    val numOfLines = StdIn.readInt()
    def words: Seq[String] = for (_ <- 1 to numOfLines) yield StdIn.readLine()
    def sort = words.map
    {
        case word => word
    }
    sort.foreach(println)
}

字符串

相关问题