本文共 1537 字,大约阅读时间需要 5 分钟。
Scala_1.控制台打印,变量定义,函数定义
package com.liusimport scala.util.control._object HelloWorld { def main(args: Array[String]): Unit = {//// printStr() //打印输出// variablePractice() //变量练习// strOperation() //字符串操作// println(functionName(12,5)) //函数操作 } /** * 打印练习 */ def printStr():Unit={ println("Hello World!") println(10) print("Hello World!") } /** * 变量练习 */ def variablePractice():Unit={ val x =10 var y = 10 y =20 val z:Int = 10 val a:Double = 1.0 val b:Double = 10.0 println("Hello World".length) println("Hello World".substring(2,6)) println("Hello World".replace("H","3")) println("Hello World".take(5)) //Hello println("Hello World".drop(5)) // World } /** * 字符串操作 */ def strOperation():Unit={ //s"" val n = 45 println(s"We have $n apples") val c = Array(11,9,6) println(s"My Second daughter is ${c(0)-c(2)} years old.") println(s"We have double the amount of ${n/2.0} in apples.") println(s"Power of 2:${math.pow(2,2)}") //f"" println(f"Power of 5:${math.pow(5,2)}%1.0f") println(f"Square of 122:${math.sqrt(122)}%1.4f") //raw"" println(raw"New line feed:\n.Carriage return:\r.") println("They stood outside the \"Rose and Crown\"") val html = """""" println(html) } /** * 函数练习 * @param a * @param b * @return */ def functionName(a:Int,b:Int):Int={// println(a+b) return a+b; }}
转载地址:http://ysnq.baihongyu.com/