//不能将lazy关键字用于var变量
scala> lazy var TestString2="Goodbye Scala"
<console>:1:error:lazy not allowed here. Only vals can be lazy
Lazy var TestString2="Goodbye Scala"
代码3.13
//特殊字符”π”是一个合法的Scala标识符
scala> val π =3.14159
π:Double = 3.14159
//特殊字符”$”是一个合法的Scala标识符
scala> val $ ="USD currency symbol"
$:String = USD currency symbol
//”o_0 ”是一个合法的Scala标识符
scala> val o_0 ="Hmm"
o_0:String =Hmm
//值名”50cent”是不合法的,因为标识符不能以数字开头。在这里,编译器最初会把这个名字解析为一个数字,解析到字母”c”时会出错
scala> val 50cent="$0.50"
<console>:1: error: Invalid literal number
val 50cent="$0.50"
^
//值名”a.b”是不合法的,因为点号不是操作符字符
scala> val a.b=25
<console>:11: error: not found: value a
val a.b=25
^
//加上反引号重写就可以解决问题
scala> val `a.b`=4
a.b: Int = 4
代码3.14
//十六进制定义法
scala> val x=0x29
x:Int =41
//十进制定义法
scala> val y=41
y:Int =41
代码3.15
//要定义Float类型浮点数,需要在浮点数后面加F或f
scala> val floatNumber=3.14159F
floatNumber: Float=3.14159
//小写的f也可以
scala> val floatNumber=3.14159f
floatNumber: Float=3.14159
代码3.16
//Double类型定义,直接输入浮点数,编译器会将其自动推断为Double类型
scala> val doubleNumber=3.14159
doubleNumber:Double=3.14159
代码3.17
//浮点数指数表示法,e也可以是E,0.314159e1与0.314159*10等同
scala> val floatNumber=0.314159e1
floatNumber: Double=3.14159
代码3.18
//字符定义,用' '将字符包裹
scala> var charLiteral='A'
charLiteral:Char=A
代码3.19
//通过转义符\进行双引号的定义
scala> var x='\"'
x:Char = "
//通过使用Unicode编码进行双引号的定义
scala> var y='\u0022'
y:Char = "
代码3.20
//字符串变量用双引号""包裹
scala> val helloWorld="Hello World"
helloWorld:String =Hello World
代码3.21
//要定义"Hello World",可以加入转义符\
scala> val helloWorld2="\"Hello World\""
helloWorld2:String ="Hello World"
代码3.22
//直接定义Boolean类型变量
scala> var x=true
x:Boolean = true
//含逻辑表达式的Boolean类型变量
scala> var y=2>3
y:Boolean = false
代码3.23
//实际上调用了(5).+(3)
scala> val sum1 = 5+3
sum1: Int = 8
//可以发现,写成方法调用的形式,和上面得到相同的结果
scala> val sum2 = (5).+(3)
sum2: Int = 8
代码3.24
//定义整型变量i
scala> var i = 5
i: Int = 5
//将i递增
scala> i += 1
scala> println(i)
6
//Int类型
scala> 1 to 10 by 2
res3: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
//Long类型
scala> 1L to 10L by 2
res4: scala.collection.immutable.NumericRange[Long] = NumericRange(1, 3, 5, 7, 9)
//BigInt类型
scala> BigInt(1) to BigInt(10) by 2
res5: scala.collection.immutable.NumericRange[BigInt] = NumericRange(1, 3, 5, 7, 9)
代码3.28
//Float类型
scala> 0.5f to 5.9f by 0.8f
res7: scala.collection.immutable.NumericRange[Float] = NumericRange(0.5, 1.3, 2.1, 2.8999999, 3.6999998, 4.5, 5.3)
//Double类型
scala> 0.5 to 5.9 by 0.8
res8: scala.collection.immutable.NumericRange[Double] = NumericRange(0.5, 1.3, 2.1, 2.9000000000000004, 3.7, 4.5, 5.3)
//BigDecimal类型
scala> BigDecimal(0.5) to BigDecimal(5.9) by 0.8
res9: scala.collection.immutable.NumericRange.Inclusive[scala.math.BigDecimal] = NumericRange(0.5, 1.3, 2.1, 2.9, 3.7, 4.5, 5.3)
代码3.29
object Demo {
def main(args: Array[String]) {
var x = 10
if( x < 20 ){
println("This is if statement")
}
}
}
代码3.30
object Demo {
def main(args: Array[String]) {
var x = 30
if( x < 20 ){
println("This is if statement")
} else {
println("This is else statement")
}
}
}
代码3.31
object Demo {
def main(args: Array[String]) {
var x = 30
if( x == 10 ){
println("Value of X is 10")
} else if( x == 20 ){
println("Value of X is 20")
} else if( x == 30 ){
println("Value of X is 30")
} else{
println("This is else statement")
}
}
}
代码3.32
object Demo {
def main(args: Array[String]) {
var x = 30
var y = 10
if( x == 30 ){
if( y == 10 ){
println("X = 30 and Y = 10")
}
}
}
}
代码3.33
object Demo {
def main(args: Array[String]) {
var i = 9
while (i > 0) {
i -= 3
printf("here is %d\n",i)
}
}
}
代码3.34
object Demo {
def main(args: Array[String]) {
var i = 9
do{
i -= 3;
printf("here is %d\n",i)
}while (i > 0)
}
}
代码3.35
for (i <- 1 to 5) println("i="+i)
代码3.36
object Demo {
def main(args: Array[String]) {
for(i<-1 to 5 if (i>3)){
for(j<-5 to 7 if (j==6)){
println("i="+i+",j="+j)
}
}
}
}
代码3.37
object Demo {
def main(args: Array[String]) {
println(matchTest(3))
}
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many"
}
}
代码3.38
object Demo {
def main(args: Array[String]) {
println(matchTest(5))
}
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case 3 => "three"
case unexpected => unexpected + " is Not Allowed"
}
}
代码3.39
object Demo {
def main(args: Array[String]) {
println(matchTest("two"))
println(matchTest("test"))
println(matchTest(1))
}
def matchTest(x: Any): Any = x match {
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
case _ => "many"
}
}
代码3.40
for (elem <- List(1,2,3,4)){
elem match {
case _ if (elem %2 == 0) => println(elem + " is even.")
case _ => println(elem + " is odd.")
}
}
代码3.41
object Demo {
def main(args: Array[String]) {
val alice = new Person("Alice", 25)
val bob = new Person("Bob", 32)
val charlie = new Person("Charlie", 32)
for (person <- List(alice, bob, charlie)) {
person match {
case Person("Alice", 25) => println("Hi Alice!")
case Person("Bob", 32) => println("Hi Bob!")
case Person(name, age) => println(
"Age: " + age + " year, name: " + name + "?")
}
}
}
case class Person(name: String, age: Int)
}
object Demo {
def main(args: Array[String]) {
var myList = Array(1.9, 2.9, 3.4, 3.5)
// 直接数组遍历输出所有元素
for ( x <- myList ) {
println( x )
}
// 所有元素求和
var total = 0.0;
//索引遍历所有元素,进行累加操作
for ( i <- 0 to (myList.length - 1)) {
total += myList(i)
}
println("Total is " + total)
// 索引遍历所有元素,寻找数组最大值
var max = myList(0)
for ( i <- 1 to (myList.length - 1) ) {
if (myList(i) > max) max = myList(i)
}
println("Max is " + max);
}
}
代码3.44
import Array._
object Demo {
def main(args: Array[String]) {
var myList1 = Array(1.9, 2.9, 3.4, 3.5)
//生成新的数组myList2
var myList2 = for(x<-myList1)yield x+1
// 输出所有数组元素
for ( x <- myList2) {
println( x )
}
}
}
代码3.45
import Array._
object Demo {
def main(args: Array[String]) {
//使用Range生成数组
var myList1 = range(10, 20, 2)
var myList2 = range(10,20)
// 打印所有数组元素
for ( x <- myList1 ) {
print( " " + x )
}
println()
for ( x <- myList2 ) {
print( " " + x )
}
}
}
代码3.46
import Array._
object Demo {
def main(args: Array[String]) {
//定义三行三列的整型二维数组
var myMatrix = ofDim[Int](3,3)
//给各元素赋值
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
myMatrix(i)(j) = j
}
}
// 打印二维数组
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
print(" " + myMatrix(i)(j))
}
println()
}
}
}