21.scala编程思想笔记——条件表达式

108 阅读1分钟

21.scala编程思想笔记——条件表达式

欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…
源码下载连接请见第一篇笔记。\

典型的模式从if开始,后面按照需要跟着多个else if子句。

然后以else结尾。

例如:

import com.atomicscala.AtomicTest._

 

def checkTruth(

  exp1:Boolean,exp2:Boolean):String = {

  if(exp1&& exp2) {

    "Bothare true"

  }

  else if(!exp1&& !exp2) {

    "Bothare false"

  }

  else if(exp1){

   "First: true, second: false"

  }

  else {

   "First: false, second: true"

  }

}

 

checkTruth(true || false, true) is

  "Bothare true"

checkTruth(1 > 0 && -1 < 0, 1 == 2) is

  "First:true, second: false"

checkTruth(1 >= 2, 1 >= 1) is

  "First:false, second: true"

checkTruth(true && false,false &&true) is

"Both are false"

执行如下:

[root@localhost examples]# scala -nocompdaemonCheckTruth.scala

Both are true

First: true, second: false

First: false, second: true

Both are false