Input 带有while和do的Scala输入
我有以下代码片段:Input 带有while和do的Scala输入,input,Input,我有以下代码片段: val input = scala.io.StdIn.readLine("Please enter a list of values separated by a comma:") val input2 = input.split(",").map(x=>x.trim).toList def chooseOption() = { val input3= scala.io.StdIn.readLine("Enter your command. You can
val input = scala.io.StdIn.readLine("Please enter a list of values separated by a comma:")
val input2 = input.split(",").map(x=>x.trim).toList
def chooseOption() = {
val input3= scala.io.StdIn.readLine("Enter your command. You can choose between list, maxLength, averageLength and exit: ")
input3 match{
case "list" => for (entry <- input2) println(entry)
case "maxLength" =>for (entry <- input2.reduce((x,y)=> if (x.length()>=y.length()) x else y)) print(entry)
case "averageLength" => {println("%.1f".format(input2.map(_.length.asInstanceOf[Double]).reduce((x,y) => x+y)/input2.length.asInstanceOf[Double]))}
case "exit" => {sys.exit()}
case _ => println("unknown command")}}
chooseOption()
val input=scala.io.StdIn.readLine(“请输入以逗号分隔的值列表:”)
val input2=input.split(“,”).map(x=>x.trim).toList
def chooseOption()={
val input3=scala.io.StdIn.readLine(“输入您的命令。您可以在list、maxLength、averageLength和exit之间进行选择:”)
输入3匹配{
案例“列表”=>用于打印(条目)
大小写“averageLength”=>{println(%.1f).format(input2.map(u.length.asInstanceOf[Double]).reduce((x,y)=>x+y)/input2.length.asInstanceOf[Double]))
案例“exit”=>{sys.exit()}
case{=>println(“未知命令”)}
chooseOption()
我的问题是,我只能键入一次类似于list,maxLength这样的操作
def chooseOption()
但我希望它允许用户进入,只要他不进入退出
在仔细研究了我的问题之后,现在我自己找到了这个问题的答案。 这是相同的代码,但只要输入不是“退出”,就调用函数
chooseOption()
val input=scala.io.StdIn.readLine(“请输入以逗号分隔的值列表:”)
val input2=input.split(“,”).map(x=>x.trim).toList
def chooseOption()={
val input3=scala.io.StdIn.readLine(“输入您的命令。您可以在list、maxLength、averageLength和exit之间进行选择:”)
输入3匹配{
案例“列表”=>for(条目如果(x.length()>=y.length())x else y)打印(条目);选择选项()
大小写“averageLength”=>{println(%.1f).format(input2.map({uu.length.asInstanceOf[Double]).reduce((x,y)=>x+y)/input2.length.asInstanceOf[Double]);chooseOption()
案例“exit”=>{sys.exit()}
case{=>println(“未知命令”)};chooseOption()
chooseOption()
val input = scala.io.StdIn.readLine("Please enter a list of values separated by a comma:")
val input2 = input.split(",").map(x=>x.trim).toList
def chooseOption() = {
val input3= scala.io.StdIn.readLine("Enter your command. You can choose between list, maxLength, averageLength and exit: ")
input3 match{
case "list" => for (entry <- input2) println(entry) ; chooseOption()
case "maxLength" =>for (entry <- input2.reduce((x,y)=> if (x.length()>=y.length()) x else y)) print(entry) ; chooseOption()
case "averageLength" => {println("%.1f".format(input2.map(_.length.asInstanceOf[Double]).reduce((x,y) => x+y)/input2.length.asInstanceOf[Double]))} ; chooseOption()
case "exit" => {sys.exit()}
case _ => println("unknown command")}} ; chooseOption()
chooseOption()