1. 计算级数

import scala.io.StdIn.readLine
object test1 {
  def main(args: Array[String]): Unit = {
    val q = readLine("请输入一个大于0的数:").toDouble
    val result = calculateSum(q)
    println(s"Sn=$result")
  }

  def calculateSum(q: Double): Double = {
    var n = 1.0
    var sum = 0.0
    while (sum < q) {
      sum += (n+1) / n
      n += 1
    }
    sum
  }
}

posted @ 2024-02-01 16:28  会秃头的小白  阅读(4)  评论(0编辑  收藏  举报