Scala001_基础语法_数据类型_条件控制与循环

1 声明变量

声明val变量:可以声明val变量来存放表达式的计算结果。
后续这些变量可以继续使用,但是变量声明后,是无法改变它的值的。
声明var变量:如果要声明值可以改变的引用,可以使用var变量。
但是在scala程序中,通常建议使用val,因此比如类似于spark的大型复杂系统中,需要大量的网络传输数据,如果使用var,可能会担心被错误更改。

2 数据类型

2.1 基本数据类型:

Byte、Char、Short、Int、Long、Float、Double、Boolean

2.2 类型的加强版类型:

1.to(10)
基本操作符:
1 + 1 ,可以写做 1.+(1)
1.to(10),可以写做 1 to 10
scala中没有提供++、–操作符,我们只能使用+和-,比如counter = 1,counter++是错误的必须写成counter += 1
函数调用方式:

2.3 函数调用方式

不同的一点,如果调用函数时,不需要传递参数,则scala允许调用函数时省略括号。

scala> import scala.math._
import scala.math._

scala> sqrt(2)
res2: Double = 1.4142135623730951

scala> pow(2,3)
res3: Double = 8.0

scala> min(3,Pi)
res4: Double = 3.0

scala> "Hello World".distinct
res6: String = Helo Wrd

2.4 apply函数

scala中的apply函数是非常特殊的一种函数,在scala的object中,可以声明apply函数。而使用“类名()”的形式,其实就是“类名.apply()”的一种缩写。通常使用这种方式来构造类的对象,而不是使用“new 类名()”的方式。
例如,“Hello World”(6),因为在StringOps类中有def apply(n:Int):Char的函数定义,所有“Hello World”(6),实际上是“Hello World”.apply(6)的缩写。
例如:Array(1,2,3,4),实际上是用Array object的apply()函数来创建Array类的实例,也就是一个数组。

scala> "Hello world"(6)
res7: Char = w

scala> "Hello world".apply(6)
res8: Char = w

scala> Array(1,2,3,4)
res9: Array[Int] = Array(1, 2, 3, 4)

scala> Array.apply(1,2,3,4)
res10: Array[Int] = Array(1, 2, 3, 4)

3 if表达式

3.1 if表达式的定义

scala> val age = 30
age: Int = 30

scala> if(age > 18) 1 else 0
res11: Int = 1

scala> val isAdult = if(age > 18) 1 else 0
isAdult: Int = 1

scala> var isAdult = -1
isAdult: Int = -1

scala> if (age > 18) isAdult = 1 else isAdult=0

scala> isAdult
res14: Int = 1

3.2 if表达式的类型推断

由于if表达式是有值的,而if和else子句的值类型可能不同,此时if表达式的值是什么类型呢?scala会自动进行推断,取两个类型的公共父类型。
Any是String和Int的公共父类型。

scala> if(age > 18) 1 else 0
res15: Int = 1

scala> if(age > 18) "adult" else 0
res16: Any = adult

如果if后面没有else,则默认else的值是Unit,也用()表示,类似于java中的void或者null。
Any是String和Unit的公共父类型。

scala> if(age < 12) "children"
res17: Any = ()

scala> age
res18: Int = 30

scala> if(age < 12) "children" else()
res19: Any = ()

3.3 if语句可以放在多行里

可以使用{}的方式,比如以下方式,或者使用:paste和ctrl+D的方式

#第一种方式
scala> if(age < 12 ){
     | "children"} else ()
res20: Any = ()

scala> if(age < 12)
     | "children" else ()
res21: Any = ()

#错误的编写方式
scala> if (age < 12) "childern
<console>:1: error: unclosed string literal
if (age < 12) "childern
              ^
#第二种方式
scala> :paste
// Entering paste mode (ctrl-D to finish)

if(age < 12)
"children"
else
"adult"

// Exiting paste mode, now interpreting.

res22: String = adult

scala>

4 语句终结符、快表达式

默认情况下,scala不需要语句终结符,默认将每一行作为一个语句

4.1 一行放多条语句

如果一行要放多条语句,多条语句用 ; 隔开

scala> var a,b,c = 0
a: Int = 0
b: Int = 0
c: Int = 0

scala> if(a < 1) b = b + 1;c = c + 1
c: Int = 1

另一种方式,进入粘贴模式

scala> :paste
// Entering paste mode (ctrl-D to finish)

if(a<1){
b=b+1
c=c+1
}

// Exiting paste mode, now interpreting.


scala> b
res25: Int = 2

scala> c
res26: Int = 2

4.2 块表达式

快表达式,指的就是{}中的值,其中可以包含多条语句,最后一个语句的值就是块表达式的返回值。

scala> val d = if(a < 1) { b = b + 1;c = c +1; b +c}
d: AnyVal = 6

5 输入和输出

5.1 print、println、printf

printf格式化,后面的变量替换占位符

scala> print("Hello World")
Hello World
scala> println("Hello World")
Hello World

scala> printf("Hello,my name is %s,I'm %d years old","Leo",30)
Hello,my name is Leo,I'm 30 years old

5.2 readLine

scala> readLine()
warning: there was one deprecation warning; re-run with -deprecation for details
res31: String = Hello world,I am Leo

scala>

5.3 练习

提示,输入名字,Leo被readLine函数读到,返回值,返回值被赋予给name这个变量。

scala> if(age > 18){
     | printf("Hi,%s ,you are %d years old,so you are legal to come here",name,age)
     | }else{
     | printf("Sorry,%s boy,you are only a child,you are %d years old.you are illegal to com here.",name,age)
     | }
(Hi,%s ,you are %d years old,so you are legal to come here,Leo,30)
scala>
scala> :paste
// Entering paste mode (ctrl-D to finish)

if(age > 18){
 printf("Hi,%s,you are %d years old,so you are legal to come here",name,age)
}else{
 printf("Sorry,boy %s,you are only %d years old,you are illegal to come here")
}

// Exiting paste mode, now interpreting.

Hi,Leo,you are 30 years old,so you are legal to come here
scala>

6 循环

6.1 whild do循环

遍历1到10

scala> var n = 10
n: Int = 10

scala> :paste
// Entering paste mode (ctrl-D to finish)

while(n > 0){
 print(n + " ")
 n -= 1
}

// Exiting paste mode, now interpreting.

10 9 8 7 6 5 4 3 2 1
scala>

6.2 scala没有for循环,只能使用while替代for,或者使用简易版的for语句

简易版的for

scala> var n = 10
n: Int = 10

scala> for(i <- 1 to n) print(i + " ")
1 2 3 4 5 6 7 8 9 10
scala>

或者使用until,表示不达到上限

scala> for(i <- 1 until 10) print(i + " ")
1 2 3 4 5 6 7 8 9
scala>

对字符串进行遍历,类似于java的增强for

scala> for(c <- "Hello World") print(c + " ")
H e l l o   W o r l d
scala>

6.3 跳出循环语句

scala没有提供类似于java的break语句
但是可以使用boolean类型变量、return或者Breaks的break函数来替代使用。

scala> import scala.util.control.Breaks._
import scala.util.control.Breaks._

scala> :paste
// Entering paste mode (ctrl-D to finish)

breakable{
 var n = 10
 for(c <- "Hello world"){
   if(n == 5) break;
   print(c)
   n -= 1
 }
}

// Exiting paste mode, now interpreting.

Hello
scala>

6.4 多重for循环:九九乘法表

scala> :paste
// Entering paste mode (ctrl-D to finish)

for(i <- 1 to 9;j <- 1 to 9){
 if(j == 9){
  println( i*j )
 }else{
  print( i*j + " ")
 }
}

// Exiting paste mode, now interpreting.

1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81

scala>
scala> :paste
// Entering paste mode (ctrl-D to finish)

for(i <- 1 to 9;j <- 1 to 9){
 if( j >= i ){
  printf("%d * %d = " + i*j + " ",i,j)
  if (j==9){
   println()
  }
 }
}

// Exiting paste mode, now interpreting.

1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9
2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18
3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36
5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45
6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54
7 * 7 = 49 7 * 8 = 56 7 * 9 = 63
8 * 8 = 64 8 * 9 = 72
9 * 9 = 81

scala>

6.5 if守卫:取偶数

for循环中直接加if判断

scala> for(i <- 1 to 10 if i % 2 == 0)println(i)
2
4
6
8
10

scala>

6.6 if推导式:构造集合

遍历1到10,每个数据保存到变量i中,通过yield关键词,把每个元素放到一个集合中,这种方式可以直接构成一个集合。

scala> for(i <- 1 to 10 ) yield i
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值