刚开始接触scala,觉得语法简单,一时兴起就写了两个简单的例子。
1 class Test11 { 2 3 def forTest1(): Unit = { 4 for (i <- 1 to 9) { 5 for (j <- 1 to i) { 6 print(j+"*"+i+"="+i*j+" "); 7 } 8 println(); 9 } 10 } 11 12 def forTest2(a:Int): Unit = { 13 for (i <- 1 to a) { 14 for (j <- 1 to a-i) { 15 print(" "); 16 } 17 for (j <- 1 to i) { 18 print("* "); 19 } 20 println(); 21 } 22 } 23 } 24 25 object Test { 26 def main(args: Array[String]): Unit = { 27 println("打印乘法口诀") 28 println("------------------------------------"); 29 (new Test11).forTest1(); 30 println("------------------------------------"); 31 var a: Int = 9; 32 println("打印"+a+"层金字塔") 33 println("------------------------------------") 34 (new Test11).forTest2(a); 35 println("------------------------------------") 36 37 } 38 }
时间: 2024-09-30 05:44:20