scala笔记第一天(windows) (变量,循环)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/TylerPY/article/details/102554722

java与scala的关系

Scala是基于Java虚拟机,也就是JVM的一门编程语言。所有Scala的代码,都需要经过编译为字节码,然后交给Java虚拟机运行。

Scala解释器的使用

REPL(scala解释器)
在这里插入图片描述

声明变量

在这里插入图片描述

数据类型与操作符

在这里插入图片描述

函数调用与apply()函数

函数调用方式
调用函数首先需要导包

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

列几个函数

scala> sqrt(2)
res11: Double = 1.4142135623730951

scala> sqrt(3)
res12: Double = 1.7320508075688772

scala> pow(2,2)
res13: Double = 4.0

scala> min(2,Pi)
res16: Double = 2.0

scala> min(4,Pi)
res17: Double = 3.141592653589793

注意在Scala中调用函数时,如果不需要传递参数,则scala调用函数可省略括号例:

scala> "Hello Word".distinct
res14: String = Helo Wrd

apply函数
Scala中的apply函数是非常特殊的一种函数,在Scala的object中,可以声明apply函数。而使用“类名()”的形式,其实就是“类名.apply()”的一种缩写。通常使用这种方法来构造类的对象,而不是使用“new 类名()”的方式。

scala> "Hello Word"(6)
res18: Char = W

scala> "Hello Word"(4)
res19: Char = o

上面是apply()的缩写

scala> "Hello Word".apply(6)
res20: Char = W

scala> "Hello Word".apply(4)
res21: Char = o

if表达式

定义
在scala中,if或else最后一行语句返回的值,可以赋值给一个变量。

scala> val age = 30;if(age>18) 1 else 0
age: Int = 30
res22: Int = 1

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

scala> isage
res23: Int = 0

if表达式的类型推断
如果if和else子句的值类型可能不同,scala会自动进行推断,取两个类型的公共父类(Any )。

扫描二维码关注公众号,回复: 7573616 查看本文章
scala> if(age>18 ) 1 else 0
res24: Int = 1

scala> if(age>18 ) "aaa" else 0
res25: Any = aaa

如果if后面没有写else,则默认else的值是Unit,也用()表示,类似于Java中的void或者null。

scala> if(age<18)"qqq" else()
res27: Any = ()

scala> if(age<18)"qqq"
res28: Any = ()

if语句在多行中
默认情况下,REPL只能解释一行语句,但是if表达式通常需要放在多行。可使用(:paste和ctrl+ D 的方式

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

var age=21
if(age<18){
"about"
}else if(age<20)"teenager" else "haha"

// Exiting paste mode, now interpreting.

age: Int = 21
res33: String = haha

语句终结符,快表达式
默认情况下,scala不需要语句的终结符,默认将每一行作为一个,
一行放多条语句必须使用分号分割。

scala> var a,b,c = 0; if(a<10){b=b+1;c=c+1}
a: Int = 0
b: Int = 1
c: Int = 1

还可以使用花括号的方式

scala> var a,b,c = 0; if(a<10){
     | b = b+4
     | c = c+5}
a: Int = 0
b: Int = 4
c: Int = 5

还可以使用(:paste和ctrl+ D 的方式

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

var a,b,c=1
if(a<10){
b= b +7
c= c +7
}

// Exiting paste mode, now interpreting.

a: Int = 1
b: Int = 8
c: Int = 8

输入和输出

scala> println("Hello Woreda");print("Hello Tyler")
Hello Woreda
Hello Tyler

在这里插入图片描述

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

综合案例

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

val name = readLine("Welcome to Game House. Please tell me your name: ")
print("Thanks. Then please tell me your age: ")
val age = readInt()
if(age > 18) {
  printf("Hi, %s, you are %d years old, so you are legel to come here!", name, age)
} else {
  printf("Sorry, boy, %s, you are only %d years old. you are illegal to come here!", name, age)
}

// Exiting paste mode, now interpreting.

循环

while循环

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

var n =10
while(n>0){
println(n)
n-=1
}

// Exiting paste mode, now interpreting.

10
9
8
7
6
5
4
3
2
1
n: Int = 0

for循环
简易for

scala> var n=10;for(i <-1 to n)println(i)
1
2
3
4
5
6
7
8
9
10
n: Int = 10

使用until代替to (to包含最后一个函数,until不包含)

scala> var n=10;for(i <-1 until n)println(i)
1
2
3
4
5
6
7
8
9
n: Int = 10

对字符串遍历

scala> for(c <- "Hello ad")print(c)
Hello ad
scala> for(c <- "Hello ad")println(c)
H
e
l
l
o

a
d

跳出循环语句

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 WORD"){
if(n==5) break;
print(c)
n-=1
}
}

// Exiting paste mode, now interpreting.

Hello

多重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 + "=" +i*j)
}else{
print(i + "*"+ j + "=" +i*j + "  " )
}
}

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

if守卫,取偶数

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

for推导式:构造集合

scala> for(i <- 1 to 10) yield i
res70: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

猜你喜欢

转载自blog.csdn.net/TylerPY/article/details/102554722
今日推荐