AI随笔-scala(1)

xiaoxiao2021-04-18  29

object learn { def main(args: Array[String]): Unit = { println(myPower(2,4)) } @annotation.tailrec def myPower(x:Int,n:Int,t:Int=1):Int={ if (n<1) t else myPower(x,n-1,x*t) } } 16 object learn { def main(args: Array[String]): Unit = { println(myPower(2,4)) println(myAdd(2,5)) println(myAdd(2)) println(mySum(1,2,3,4,5)) } @annotation.tailrec def myPower(x:Int,n:Int,t:Int=1):Int={ if (n<1) t else myPower(x,n-1,x*t) } def myAdd(x:Int,y:Int=0)={ val result:Int=x+y s"$x add $y =$result" } def mySum(nums:Int*)={//可变参数 var sumNum=0 for (num<-nums){ sumNum+=num } sumNum } } 16 2 add 5 =7 2 add 0 =2 15

编译成jar文件

MacBook-Air:Documents myhaspl$ mkdir hello MacBook-Air:Documents myhaspl$ cd hello MacBook-Air:hello myhaspl$ ls MacBook-Air:hello myhaspl$ pwd /Users/lxxxxx/Documents/hello MacBook-Air:hello myhaspl$ vim HelloWorld.scala MacBook-Air:hello myhaspl$ cat HelloWorld.scala object HelloWorld{ def main(args: Array[String]) { println("Hello, SBT") } } MacBook-Air:hello myhaspl$ ls HelloWorld.scala MacBook-Air:hello myhaspl$ sbt [warn] No sbt.version set in project/build.properties, base directory: /Users/lzzzz/Documents/hello [info] Set current project to hello (in build file:/Users/liuxing/Documents/hello/) [info] sbt server started at local:///Users/zzzz/.sbt/1.0/server/acaf9a819c75648cf68d/sock sbt:hello> run [info] Updating ... [info] Done updating. [info] Compiling 1 Scala source to /Users/zzz/Documents/hello/target/scala-2.12/classes ... [info] Non-compiled module 'compiler-bridge_2.12' for Scala 2.12.7. Compiling... [info] Compilation completed in 15.127s. [info] Done compiling. [info] Packaging /Users/zzzz/Documents/hello/target/scala-2.12/hello_2.12-0.1.0-SNAPSHOT.jar ... [info] Done packaging. [info] Running HelloWorld Hello, SBT [success] Total time: 30 s, completed Oct 10, 2018 8:02:55 PM sbt:hello> MacBook-Air:hello myhaspl$ vim build.sbt MacBook-Air:hello myhaspl$ cat build.sbt name := "hello" // 项目名称 organization := "myhaspl.com" // 组织名称 version := "0.0.1" // 版本号 scalaVersion := "2.12.7" // 使用的Scala版本号 MacBook-Air:hello myhaspl$ sbt:hello> compile [info] Updating ... [info] Done updating. [success] Total time: 1 s, completed Oct 10, 2018 8:20:26 PM sbt:hello> package [info] Packaging /Users/xxxxx/Documents/hello/target/scala-2.12/hello_2.12-0.0.1.jar ... [info] Done packaging. [success] Total time: 0 s, completed Oct 10, 2018 8:20:35 PM MacBook-Air:scala-2.12 myhaspl$ scala hello_2.12-0.0.1.jar Hello, SBT
转载请注明原文地址: https://www.6miu.com/read-4819882.html

最新回复(0)