Scala选择分支if else

 1 package com.atguigu.chapter03
 2 
 3 import scala.io.StdIn
 4 
 5 /**
 6   * Scala 中 if else 表达式其实是有返回值的,具体返回值取决于满足条件的
 7   * 代码体的最后一行内容。
 8   */
 9 object TestIfElse {
10   def main(args: Array[String]): Unit = {
11     println("input age")
12     var age = StdIn.readInt()
13     val res : String = if (age >12 && age< 18){
14       "少年"
15     }else {
16       if(age>=18 && age<50){
17         "中年"
18       }else{
19         "老年"
20       }
21     }
22     println("result:   "+res)
23   }
24 }

 

posted @ 2021-12-30 22:22  靠谱杨  阅读(52)  评论(0编辑  收藏  举报