AI随笔-scala(1)



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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值