训练题2

20 阅读1分钟

image.png

object Base65 {
  def main(args: Array[String]): Unit = {
    import scala.io.StdIn

    // 定义判断闰年的函数
    def isLeapYear(year: Int): Boolean = {
      (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
    }

    def inputUntilLeapYear(): Unit = {
      var year = 0
      do {
        print("请输入年份:")
        year = StdIn.readInt()
      } while (!isLeapYear(year))
      println(s"输入的闰年是:$year")
    }

    inputUntilLeapYear()
  }
}

运行结果:

image.png

image.png

object Base66 {
  def main(args: Array[String]): Unit = {
    val scores = List(100, 51, 55, 67, 59, 89)
    val newScores = scores.map(score => if (score >= 56 && score <= 59) 60 else score)
    println(newScores)
  }
}

运行结果:

image.png