object test {
def main(args: Array[String]): Unit = {
val id ="42032220080903332x"
//1.生日是?
//字符串截取
val birthday =id.substring(10,14) //不包括终点下标
println(birthday)
val year =id.substring(6,10) //
println(year)
//性别:倒数第2位
//如果是奇数:boy,偶数:girl
val gender = id.charAt(16)
println(gender)
if(gender.toInt % 2 ==0) {
println("性别是:女生")
}
else {
println("性别是:男生")
}
//前两位表示省份
val province = id.substring(0,2).toInt
val m1 = Map((41,"河南"),(42,"湖北"),(43,"湖南"),(44,"广东"))
println(s"籍贯是:${m1.get(province)}")
//验证身份证号是否合法
//每一位的积重
val weights =Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2)
val checkCodes ="10x98765432"
var sum = 0
for (i <- 0 to 16){
sum += id(i).toInt * weights(i)
}
if(id.charAt(17)==checkCodes.charAt(sum%11)){
println(s"校检成功")
}else{
println(s"校检失败")
}
val str1 =
"""
|静夜思
|李白
|床前明月光
|疑是地上霜
|"""
}
}
map的遍历
import scala.io.Source
object test32 {
def main(args: Array[String]): Unit = {
val content = Source.fromFile("1.text").mkString
println(content)
//把字符串中的每个单词,拆出来
//正则表达式
//\\W+:大学字符串表示:非字(不是一个的字符。例如:空格 逗号 句号 换行...)
//+:一个或者多个
val arr =content.split("\\W+") //
arr.foreach(println)
//如果有一个字符串组:
val arr1 =Array("thank","you","very","much","thank","very")
val m1 =scala.collection.mutable.Map[String,Int]()
arr1.foreach(word =>{
//检查是否出现过?
if (m1.contains(word)){
//把票数+1
m1(word) += 1
}else {
//票数为1
2
}
})
//对于m1,它是一个Map(thank:10,is:5),现在需要对它进行排序,把出现次数最多的放在最前面,然后再输出
m1.foreach{case (word,cishu) => println(s"${word}:${cishu}")}
}
}
补充