如何在 Scala 中获取用户的输入?
我想接受用户的输入。你能告诉我如何在 Scala 中以字符串形式请求用户输入吗?
I want to take input from the user. Can you please tell me how to ask for user input as a string in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在 Scala 2.11 中,使用
代替已弃用的
Console.readLine
。In Scala 2.11 use
instead of the deprecated
Console.readLine
.这是读取整数值的标准方法,
类似地
从标准输入的整行读取布尔值。
从 stdin 读取整行的 Byte 值。
从 stdin 读取整行的 Char 值。
从 stdin 读取整行的 Double 值。
从 stdin 读取整行的浮点值。
从 stdin 读取整行 Int 值。
将格式化文本打印到标准输出并从标准输入读取整行。
从标准输入读取整行。
从 stdin 读取整行的 Long 值。
从 stdin 读取整行的 Short 值。
按照格式说明符的指定从 stdin 读取结构化输入。
按照格式说明符的指定从 stdin 读取结构化输入,返回
根据格式规范,仅提取第一个值。
按照格式说明符的指定从 stdin 读取结构化输入,返回
根据格式规范,仅提取前两个值。
按照格式说明符的指定从 stdin 读取结构化输入,返回
根据格式规范,仅提取前三个值。
同样,如果您想从同一行读取多个用户输入,例如:姓名、年龄、体重,您可以使用
摘自 Scala Cookbook:Alvin Alexander 的面向对象和函数式编程的食谱的Scanner 对象
Here is a standard way to read Integer values
similarly
Reads a Boolean value from an entire line from stdin.
Reads a Byte value from an entire line from stdin.
Reads a Char value from an entire line from stdin.
Reads a Double value from an entire line from stdin.
Reads a Float value from an entire line from stdin.
Reads an Int value from an entire line from stdin.
Prints formatted text to stdout and reads a full line from stdin.
Reads a full line from stdin.
Reads a Long value from an entire line from stdin.
Reads a Short value from an entire line from stdin.
Reads in structured input from stdin as specified by the format specifier.
Reads in structured input from stdin as specified by the format specifier, returning
only the first value extracted, according to the format specification.
Reads in structured input from stdin as specified by the format specifier, returning
only the first two values extracted, according to the format specification.
Reads in structured input from stdin as specified by the format specifier, returning
only the first three values extracted, according to the format specification.
Similarly if you want to read multiple user inputs from the same line ex: name, age, weight you can use the Scanner object
abridged from Scala Cookbook: Recipes for Object-Oriented and Functional Programming by Alvin Alexander
来自 Scala maling 列表 (格式和链接已更新):
From the Scala maling list (formatting and links were updated):
这样您就可以询问输入。
This way you can ask input.
您可以使用 readLine() 获取用户字符串输入。
或者您可以使用扫描仪类来获取用户输入。
You can take a user String input using readLine().
Or you can use the scanner class to take user input.
读取用户输入的简单示例
我们也可以使用 Read Line
Simple Example for Reading Input from User
Also we can use Read Line
在 Scala 2 中:
此代码从用户获取输入并输出:
In Scala 2:
This code gets input from user and outputs it:
readLine()
可让您提示用户并以字符串形式读取他们的输入readLine()
lets you prompt the user and read their input as a String请尝试
scala> readint
请尝试这个方法
please try
scala> readint
please try this method