寒假学习5打印99乘法表

使用for循环打印99乘法表

package C04

object test3_Practice99 {
  //输出99乘法表
  def main(args: Array[String]): Unit = {
    for(i <- 1 to 9){
      for (j <- 1 to i){
        print(s"${j} * ${i} = ${i*j} \t")
      }
      println()
    }
  }
}

优化一下循环

package C04

object test3_Practice99 {
  //输出99乘法表
  def main(args: Array[String]): Unit = {
    for(i <- 1 to 9 ; j <- 1 to i){
      print(s"${j} * ${i} = ${i*j} \t")
      if (j == i) println()
    }
  }
}

结果

原文地址:https://www.cnblogs.com/dty602511/p/15768673.html