分类:
scala
package Project object 九层妖塔金字塔 { def main(args:Array[String]):Unit={ for ( i <- 1 to 9 ) { val stars = 2 * i - 1 val spaces = 9 - i println("" * spaces + "*" * stars) } for (i <- 1 to 9; stars = 2 * i - 1; spaces = 9 - i) { println("" * spaces+ "*" * stars) } } }
package Project object 九九乘法表 { def main(args:Array[String]):Unit={ for(i <- 1 to 9){ for(j <- 1 to i){ print(s"$j * $i = ${i * j}\t") } println() } //所有的代码都在内循环,则可以使用循环的嵌套 for(i <- 1 to 9; j <- 1 to i){ print(s"$j * $i = ${i * j}\t") if(i == j) println() } } }
package students object Student3 { def main(args:Array[String]):Unit={ val name:String ="alice" val age:Int=18 println(age+"岁的"+name+"在硅谷学习") println(name*3) printf("%d岁的%s",age,name) println(s"${age}岁的${name}在学习")//s+""--->表示模板字符串 val num=2.1314//默认double //val sum:Float=3.1415f println(f"The num is ${num}%2.2f")//格式化模板字符串 println(raw"The num is ${num}%2.2f") //三引号表示字符串,保持多行字符串的原格式输出 val sql=s""" |select* |from |student |where |name=${name} |""".stripMargin println(sql) } }
评价