多重循环

41 阅读1分钟
object Base25 {
  def main(args: Array[String]): Unit = {
    var n = 0
    for(i <- 1 to 10){
      for(j <- 5 to 20){
        for(k <-1 to 10){
          n +=1
          println(n,k,i,j)
        }
      }
    }
  }
}

九九乘法表

object Base26 {
  def main(args: Array[String]): Unit = {
    for(i <- 1 to 9){
      for(j <- 1 to 9){
          printf("%d*%d=%-4d",j,i,i*j)
        }
         println()
      }
    }
  }

%d表示一个占位符,要填一个整数

object Base27 {
  def main(args: Array[String]): Unit = {
    var k = 0
    for (gj <- 0 to 100) {
      for (mj <- 0 to 100) {
        var xj = 100 - gj - mj
        if (xj >= 0) {
          if (gj * 5 + mj * 3 + xj / 3 == 100) {
            k += 1
              println(k, gj, mj, xj)
          }
        }
      }
    }
  }
}