如何编写 hello world

目标

我想用 Scala 编写 hello world 程序并运行它。

步骤

  1. 新建一个目录,例如 hello_scala/

  2. 在 hello_scala/ 下创建名为 project/ 的目录,并创建 project/build.properties,内容为

    sbt.version=2.0.11
    
  3. 在 hello_scala/ 下创建 build.sbt:

    scalaVersion := "3.9.0"
    
  4. 在 hello_scala/ 下创建 Hello.scala:

     @main def main(args: String*): Unit =
       println(s"Hello ${args.mkString}")
    
  5. 在终端中进入 hello_scala/ 并运行 sbt:

    $ sbt
    
  6. 出现提示符后,输入 run:

    sbt:hello_scala> run
    
  7. 输入 exit 退出 sbt shell:

    sbt:hello_scala> exit
    

其他方式

若您赶时间,可在空目录中运行 sbt init,并选择第一个模板。