在Scala中映射用户输入,有什么好的方法可以为Map元素获取用户输入吗



我正试图通过下面的方法从用户那里获取String键和布尔值,有更好的方法吗?

val sc = new Scanner(System.in)
println("Please enter First User Name and Boolean value")
val sc1:String  = sc.next();
val sc2:Boolean = sc.nextBoolean()
println("Please enter Second User Name and Boolean value")
val sc3:String  = sc.next();
val sc4:Boolean = sc.nextBoolean()
println("Please enter Third User Name and Boolean value")
val sc5:String  = sc.next();
val sc6:Boolean = sc.nextBoolean()
val map:Map[String,Boolean] = Map(sc1->sc2,sc3->sc4,sc5->sc6)

当然。

println("Please enter names and values separated by `=`, each pair on its own line. Press ^D when done.")
val map = Source
.fromInputStream(System.in)
.getLines
.map(_.split("="))
.map { case Array(a,b) => a -> b.toBoolean }
.toMap

相关内容

  • 没有找到相关文章

最新更新