【问题标题】:Scala Console.readInt deprecatedScala Console.readInt 已弃用
【发布时间】:2017-07-12 10:39:17
【问题描述】:

scala的命令行解释器显示:

scala> Console.readInt
warning: there was one deprecation warning; re-run with -deprecation for details
res0: Int = 65

Console.readInt 真的被弃用了吗?如果是,正确的输入方式是什么?

我在 OS X 10.10.5 上使用 Scala 版本 2.11.7。

【问题讨论】:

    标签: scala


    【解决方案1】:

    在 Scala 2.11 中,您可以使用

    scala.io.StdIn.readInt()
    

    scala.io.StdIn.readLine().toInt
    

    【讨论】:

      【解决方案2】:

      按照 REPL 的建议,运行 scala -deprecation 以获取更多信息:

      scala -deprecation
      Welcome to Scala version 2.11.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_79).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala>  Console.readInt
      <console>:8: warning: method readInt in class DeprecatedConsole is deprecated: Use the method in scala.io.StdIn
                     Console.readInt
                             ^
      

      【讨论】:

        【解决方案3】:

        或者,您可以使用 import 来消除警告:

        import scala.io.StdIn._
        val name = readLine("Your name:")
        val age = readInt()
        printf("Your name: %s, your age: %d\n", name, age)
        

        【讨论】:

          【解决方案4】:

          查看Scala Api

          (Since version 2.11.0) Use the method in scala.io.StdIn
          

          它还有readInt方法

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-02-12
            • 1970-01-01
            • 2016-11-11
            • 2015-02-17
            • 1970-01-01
            • 2014-05-08
            • 1970-01-01
            • 2016-03-03
            相关资源
            最近更新 更多