Skip to content

Scala 判断语句

Scala 提供了多种条件控制结构,包括 if-else 语句、match 表达式等。与 Java 不同,Scala 中的条件语句都是表达式,都会返回值。

if-else 表达式

基本 if-else

scala
object BasicIfElse {
  def main(args: Array[String]): Unit = {
    val age = 18
    
    // 基本 if-else
    if (age >= 18) {
      println("成年人")
    } else {
      println("未成年人")
    }
    
    // if-else 作为表达式
    val status = if (age >= 18) "成年人" else "未成年人"
    println(s"状态: $status")
    
    // 单行 if-else
    val message = if (age >= 18) "可以投票" else "不能投票"
    println(message)
  }
}

多重 if-else

scala
object MultipleIfElse {
  def main(args: Array[String]): Unit = {
    val score = 85
    
    val grade = if (score >= 90) {
      "A"
    } else if (score >= 80) {
      "B"
    } else if (score >= 70) {
      "C"
    } else if (score >= 60) {
      "D"
    } else {
      "F"
    }
    
    println(s"分数: $score, 等级: $grade")
    
    // 更复杂的条件
    val temperature = 25
    val weather = if (temperature > 30) {
      "炎热"
    } else if (temperature > 20) {
      "温暖"
    } else if (temperature > 10) {
      "凉爽"
    } else {
      "寒冷"
    }
    
    println(s"温度: ${temperature}°C, 天气: $weather")
  }
}

嵌套 if-else

scala
object NestedIfElse {
  def main(args: Array[String]): Unit = {
    val age = 25
    val hasLicense = true
    val hasInsurance = true
    
    val canDrive = if (age >= 18) {
      if (hasLicense) {
        if (hasInsurance) {
          "可以开车"
        } else {
          "需要保险"
        }
      } else {
        "需要驾照"
      }
    } else {
      "年龄不够"
    }
    
    println(canDrive)
    
    // 使用逻辑运算符简化
    val canDriveSimple = if (age >= 18 && hasLicense && hasInsurance) {
      "可以开车"
    } else {
      "不能开车"
    }
    
    println(canDriveSimple)
  }
}

match 表达式

基本 match 表达式

scala
object BasicMatch {
  def main(args: Array[String]): Unit = {
    val dayOfWeek = 3
    
    val dayName = dayOfWeek match {
      case 1 => "星期一"
      case 2 => "星期二"
      case 3 => "星期三"
      case 4 => "星期四"
      case 5 => "星期五"
      case 6 => "星期六"
      case 7 => "星期日"
      case _ => "无效的日期"  // 默认情况
    }
    
    println(s"第 $dayOfWeek 天是 $dayName")
    
    // 匹配字符串
    val fruit = "apple"
    val color = fruit match {
      case "apple" => "红色"
      case "banana" => "黄色"
      case "orange" => "橙色"
      case "grape" => "紫色"
      case _ => "未知颜色"
    }
    
    println(s"$fruit 的颜色是 $color")
  }
}

带条件的 match

scala
object ConditionalMatch {
  def main(args: Array[String]): Unit = {
    val number = 15
    
    val description = number match {
      case n if n < 0 => "负数"
      case n if n == 0 => "零"
      case n if n > 0 && n < 10 => "个位正数"
      case n if n >= 10 && n < 100 => "两位正数"
      case _ => "大数"
    }
    
    println(s"$number$description")
    
    // 匹配范围
    val grade = 85
    val level = grade match {
      case g if g >= 90 => "优秀"
      case g if g >= 80 => "良好"
      case g if g >= 70 => "中等"
      case g if g >= 60 => "及格"
      case _ => "不及格"
    }
    
    println(s"分数 $grade 对应等级 $level")
  }
}

匹配类型

scala
object TypeMatching {
  def main(args: Array[String]): Unit = {
    def processValue(value: Any): String = value match {
      case s: String => s"字符串: $s"
      case i: Int => s"整数: $i"
      case d: Double => s"浮点数: $d"
      case b: Boolean => s"布尔值: $b"
      case list: List[_] => s"列表,长度: ${list.length}"
      case _ => "未知类型"
    }
    
    println(processValue("Hello"))        // 字符串: Hello
    println(processValue(42))             // 整数: 42
    println(processValue(3.14))           // 浮点数: 3.14
    println(processValue(true))           // 布尔值: true
    println(processValue(List(1, 2, 3)))  // 列表,长度: 3
    println(processValue(Array(1, 2)))    // 未知类型
  }
}

匹配集合

scala
object CollectionMatching {
  def main(args: Array[String]): Unit = {
    def analyzeList(list: List[Int]): String = list match {
      case Nil => "空列表"
      case head :: Nil => s"只有一个元素: $head"
      case head :: tail => s"头元素: $head, 尾部长度: ${tail.length}"
    }
    
    println(analyzeList(List()))           // 空列表
    println(analyzeList(List(1)))          // 只有一个元素: 1
    println(analyzeList(List(1, 2, 3)))    // 头元素: 1, 尾部长度: 2
    
    // 匹配特定模式
    def matchPattern(list: List[Int]): String = list match {
      case List(1, 2, 3) => "正好是 1, 2, 3"
      case List(1, _*) => "以 1 开头的列表"
      case List(_, _, 3) => "第三个元素是 3 的三元素列表"
      case x :: y :: _ if x > y => "前两个元素递减"
      case _ => "其他模式"
    }
    
    println(matchPattern(List(1, 2, 3)))     // 正好是 1, 2, 3
    println(matchPattern(List(1, 4, 5)))     // 以 1 开头的列表
    println(matchPattern(List(2, 1, 3)))     // 第三个元素是 3 的三元素列表
    println(matchPattern(List(5, 3, 1)))     // 前两个元素递减
  }
}

条件表达式的高级用法

Option 类型的条件处理

scala
object OptionConditionals {
  def main(args: Array[String]): Unit = {
    def findUser(id: Int): Option[String] = {
      if (id > 0) Some(s"User$id") else None
    }
    
    val userId = 5
    val user = findUser(userId)
    
    // 使用 match 处理 Option
    val message = user match {
      case Some(name) => s"找到用户: $name"
      case None => "用户不存在"
    }
    println(message)
    
    // 使用 if-else 处理 Option
    val result = if (user.isDefined) {
      s"用户名: ${user.get}"
    } else {
      "未找到用户"
    }
    println(result)
    
    // 使用 getOrElse
    val userName = user.getOrElse("匿名用户")
    println(s"用户: $userName")
  }
}

函数式条件处理

scala
object FunctionalConditionals {
  def main(args: Array[String]): Unit = {
    val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    // 使用 filter 进行条件筛选
    val evenNumbers = numbers.filter(_ % 2 == 0)
    println(s"偶数: $evenNumbers")
    
    // 使用 partition 分割
    val (evens, odds) = numbers.partition(_ % 2 == 0)
    println(s"偶数: $evens")
    println(s"奇数: $odds")
    
    // 使用 find 查找
    val firstEven = numbers.find(_ % 2 == 0)
    println(s"第一个偶数: $firstEven")
    
    // 使用 exists 检查存在性
    val hasEven = numbers.exists(_ % 2 == 0)
    println(s"包含偶数: $hasEven")
    
    // 使用 forall 检查全部
    val allPositive = numbers.forall(_ > 0)
    println(s"全部为正数: $allPositive")
  }
}

实践示例

成绩管理系统

scala
case class Student(name: String, score: Int)

object GradeSystem {
  def getGrade(score: Int): String = score match {
    case s if s >= 90 => "A"
    case s if s >= 80 => "B"
    case s if s >= 70 => "C"
    case s if s >= 60 => "D"
    case _ => "F"
  }
  
  def getStatus(score: Int): String = {
    if (score >= 60) "及格" else "不及格"
  }
  
  def analyzeStudent(student: Student): String = {
    val grade = getGrade(student.score)
    val status = getStatus(student.score)
    
    student.score match {
      case s if s >= 95 => s"${student.name}: 优秀学生 (${grade}等, $status)"
      case s if s >= 85 => s"${student.name}: 良好学生 (${grade}等, $status)"
      case s if s >= 60 => s"${student.name}: 普通学生 (${grade}等, $status)"
      case _ => s"${student.name}: 需要帮助 (${grade}等, $status)"
    }
  }
  
  def main(args: Array[String]): Unit = {
    val students = List(
      Student("张三", 95),
      Student("李四", 87),
      Student("王五", 72),
      Student("赵六", 45)
    )
    
    students.foreach(student => println(analyzeStudent(student)))
  }
}

简单的状态机

scala
sealed trait State
case object Idle extends State
case object Running extends State
case object Paused extends State
case object Stopped extends State

class StateMachine(private var currentState: State = Idle) {
  def transition(action: String): State = {
    currentState = (currentState, action) match {
      case (Idle, "start") => Running
      case (Running, "pause") => Paused
      case (Running, "stop") => Stopped
      case (Paused, "resume") => Running
      case (Paused, "stop") => Stopped
      case (Stopped, "reset") => Idle
      case (state, _) => 
        println(s"无效的转换: $state -> $action")
        state
    }
    currentState
  }
  
  def getCurrentState: State = currentState
}

object StateMachineExample {
  def main(args: Array[String]): Unit = {
    val machine = new StateMachine()
    
    println(s"初始状态: ${machine.getCurrentState}")
    println(s"启动后: ${machine.transition("start")}")
    println(s"暂停后: ${machine.transition("pause")}")
    println(s"恢复后: ${machine.transition("resume")}")
    println(s"停止后: ${machine.transition("stop")}")
    println(s"重置后: ${machine.transition("reset")}")
  }
}

最佳实践

  1. 优先使用表达式:利用 Scala 中条件语句返回值的特性
  2. 使用 match 而不是多重 if-else:当有多个条件时,match 更清晰
  3. 避免深层嵌套:使用逻辑运算符或提前返回来减少嵌套
  4. 使用 Option 处理可能为空的值:避免空指针异常
  5. 利用模式匹配的强大功能:匹配类型、集合结构等

掌握条件控制结构是编写逻辑清晰、可维护代码的基础。

本站内容仅供学习和研究使用。