今日练习

76 阅读2分钟

(四)案例实现:输入身份证号输出基本信息

编写程序,实现效果。

// 从键盘上输入一个身份证号,输出判断结果

籍贯:湖北省

性别:男

年龄:40

生日:06-11

星座:巨蟹

性格:

代码:

image.png

image.png

image.png

image.png

运行结果:

image.png

代码:

package string

import java.time.LocalDate
import java.time.format.DateTimeFormatter

object string02 {
  // 全国行政区划6位地址码映射表
  private val areaCodeMap: Map[String, String] = Map(
    "420000" -> "湖北省",
    "420200" -> "湖北省黄石市",
    "420222" -> "湖北省黄石市阳新县"
  )

  def main(args: Array[String]): Unit = {
    val idCard = "420222200508252051"

    // 身份证格式基础校验
    if (!isValidIdCard(idCard)) {
      println("⚠️ 身份证号格式非法!")
      return
    }

    // 1. 性别解析
    val genderCode = idCard.substring(16, 17).toInt
    val gender = if (genderCode % 2 == 0) "女" else "男"

    // 2. 生日解析(标准化格式)
    val birthDateStr = idCard.substring(6, 14)
    val birthDateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd")
    val birthDate = LocalDate.parse(birthDateStr, birthDateFormatter)
    val birthDateDisplay = birthDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))

    // 3. 籍贯解析
    val areaCode = idCard.substring(0, 6)
    val nativePlace = getNativePlace(areaCode)

    // 4. 精准年龄计算
    val age = calculateExactAge(birthDate)

    // 5. 星座解析(修复语法错误)
    val constellation = getConstellation(birthDate.getMonthValue, birthDate.getDayOfMonth)

    // 格式化输出
    println("=" * 40)
    println("身份证信息解析结果:")
    println("=" * 40)
    println(s"* 身份证号: $idCard")
    println(s"* 性别: $gender")
    println(s"* 生日: $birthDateDisplay")
    println(s"* 籍贯: $nativePlace")
    println(s"* 年龄: $age")
    println(s"* 星座: $constellation")
    println("=" * 40)
  }

  /**
   * 身份证格式校验(18位)
   */
  def isValidIdCard(idCard: String): Boolean = {
    val idRegex = "^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))((0[1-9])|([12]\d)|(3[01]))\d{3}[0-9Xx]$".r
    idRegex.matches(idCard)
  }

  /**
   * 动态计算精准年龄
   */
  def calculateExactAge(birthDate: LocalDate): Int = {
    val today = LocalDate.now()
    var age = today.getYear - birthDate.getYear
    if (today.getMonthValue < birthDate.getMonthValue ||
      (today.getMonthValue == birthDate.getMonthValue && today.getDayOfMonth < birthDate.getDayOfMonth)) {
      age -= 1
    }
    age
  }

  /**
   * 动态解析籍贯
   */
  def getNativePlace(areaCode: String): String = {
    areaCodeMap.getOrElse(areaCode, {
      val provinceCode = areaCode.substring(0, 2) + "0000"
      areaCodeMap.getOrElse(provinceCode, s"未知(地址码:$areaCode)")
    })
  }

  /**
   * 修复后的星座匹配逻辑(Scala 标准语法)
   * 核心修正:拆分多条件 case 语句,使用合法的模式匹配语法
   */
  def getConstellation(month: Int, day: Int): String = (month, day) match {
    // 水瓶座:1.20-2.18
    case (1, d) if d >= 20 => "水瓶座"
    case (2, d) if d <= 18 => "水瓶座"
    // 双鱼座:2.19-3.20
    case (2, d) if d >= 19 => "双鱼座"
    case (3, d) if d <= 20 => "双鱼座"
    // 白羊座:3.21-4.19
    case (3, d) if d >= 21 => "白羊座"
    case (4, d) if d <= 19 => "白羊座"
    // 金牛座:4.20-5.20
    case (4, d) if d >= 20 => "金牛座"
    case (5, d) if d <= 20 => "金牛座"
    // 双子座:5.21-6.21
    case (5, d) if d >= 21 => "双子座"
    case (6, d) if d <= 21 => "双子座"
    // 巨蟹座:6.22-7.22
    case (6, d) if d >= 22 => "巨蟹座"
    case (7, d) if d <= 22 => "巨蟹座"
    // 狮子座:7.23-8.22
    case (7, d) if d >= 23 => "狮子座"
    case (8, d) if d <= 22 => "狮子座"
    // 处女座:8.23-9.22
    case (8, d) if d >= 23 => "处女座"
    case (9, d) if d <= 22 => "处女座"
    // 天秤座:9.23-10.23
    case (9, d) if d >= 23 => "天秤座"
    case (10, d) if d <= 23 => "天秤座"
    // 天蝎座:10.24-11.22
    case (10, d) if d >= 24 => "天蝎座"
    case (11, d) if d <= 22 => "天蝎座"
    // 射手座:11.23-12.21
    case (11, d) if d >= 23 => "射手座"
    case (12, d) if d <= 21 => "射手座"
    // 摩羯座:12.22-1.19
    case (12, d) if d >= 22 => "摩羯座"
    case (1, d) if d <= 19 => "摩羯座"
    // 异常情况
    case _ => "未知星座"
  }
}