一.Scala中的数据类型和变量常量
1、注意:在Scala中,任何数据都是对象。和Java不同。
举例:
在Java中 1 是基本数据类型。
cmd中 清屏快捷键:ctrl+l
scala> 2. (Tab键查看方法)
scala> 2.
!= + << >= abs compareTo getClass isNaN isValidChar isWhole round to toDegrees toInt toShort underlying
% - <= >> byteValue doubleValue intValue isNegInfinity isValidInt longValue self toBinaryString toDouble toLong unary_+ until
& / == >>> ceil floatValue isInfinite isPosInfinity isValidLong max shortValue toByte toFloat toOctalString unary_- |
* < > ^ compare floor isInfinity isValidByte isValidShort min signum toChar toHexString toRadians unary_~
在scala中 2是对象。用法:
scala> 2.toDouble
res4: Double = 2.0
二. 数值类型:Byte,Short,Int,Long,Float,Double
-
Byte: 8位有符号数字,从-128 到 127
-
Short: 16位有符号数据,从-32768 到 32767
-
Int: 32位有符号数据 -2147483648-2147483647(20亿)
-
Long: 64位有符号数据 -9223372036854775808-9223372036854775807
字符串 String 字符 Char
java和scala对比:
scala> var s1 : String = "Hello world"
Java : String s1 = "Hello world"
scala> var s1 : String = "Hello world"
s1: String = Hello world
scala> "My name is Tom and ${s1}"
res6: String = My name is Tom and ${s1}
需要在前面加入s才可以进行插值操作
scala> s"My name is Tom and ${s1}"
res7: String = My name is Tom and Hello world
三、变量 var 和 常量 val
常量 val:
scala> val b : Int = 11
b: Int = 11
常量的值不可以改变
scala> b = 12
<console>:12: error: reassignment to val
b = 12
^
变量 var:
scala> var a : Int = 11
a: Int = 11
变量的值可以改变
scala> a = 12
a: Int = 12
scala> a=001
<console>:1: error: Decimal integer literals may not have a leading zero. (Octal syntax is obsolete.)
a=001
^
错误:十进制整数文本不能有前导零。(八进制语法已过时。)
error: Decimal integer literals may not have a leading zero. (Octal syntax is obsolete.)
scala自动类型推导:
scala> var a = 11
a: Int = 11
scala> var a = "Hello"
a: String = Hello
scala> val a = 11
a: Int = 11
索引越界:
scala> val a:Byte = 128
<console>:11: error: type mismatch;
found : Int(128)
required: Byte
val a:Byte = 128
^
四、unit类型 Nothing类型
(1)Unit 类型,相当于Java中的void,就是没有返回值。
scala> val f = ()
f: Unit = ()
小括号代表一个函数,这个函数没有返回值
(2)Nothing 类型,在执行过程中,产生Exception
scala> def myFunction = throw new Exception("Some Error")
myFunction: Nothing
scala> def myexception=throw new Exception("yufa exception")
myexception: Nothing
五、函数:是scala的头等公民
(一)scala内置函数,可以直接使用
scala> max(1,2)
<console>:12: error: not found: value max
max(1,2)
^
错误原因,没有引入math包
scala> import scala.math._
import scala.math._
scala> max(1,2)
res9: Int = 2
说明:
_ 相当于Java中的*,代表这个包下所有的东西。
res9: Int = 2
本质:定义了一个变量,res9,保存执行结果,推导出res9类型是Int。
scala> var a1 : Int = max(1,2)
a1: Int = 2
也可以省略类型声明
scala> var a2 = max(2,3)
a2: Int = 3
scala> var a=max(90,89)
a: Int = 90
(二)自定义函数: 关键字 def
格式: def 函数名称(参数列表 举例 x : Int, y : Int) : 返回值类型 = {
函数实现
}
举例:
1、求和
scala> def sum(x:Int,y:Int) : Int = x + y
sum: (x: Int, y: Int)Int
scala> sum(10,20)
res10: Int = 30
scala> var a = sum(10 , 20)
a: Int = 30
2、求阶乘 递归
scala> def myFactor(x:Int) : Int = {
| if (x <= 1)
| 1
| else
| x*myFactor(x-1)
| }
myFactor: (x: Int)Int
注意:没有return语句。函数的最后一句话就是函数的返回值。
上面例子中,有函数的分支,1 和 x*myFactor(x-1) 都有可能是函数的最后一句话。
相当于在前面都有return
scala> myFactor(5)
res11: Int = 120
3、求输入的年份是否是闰年
普通闰年:能被4整除但不能被100整除的年份为普通闰年(如 2004年就是闰年,1999年不是闰年)
世纪闰年:能被400整除的为世纪闰年(如 2000年就是世纪闰年 1900年不是世纪闰年)
scala> def isLeapYear(x:Int)={
| if(x%4 == 0 && x%100 !=0 ||x % 400 == 0)
| true
| else
| false
| }
isLeapYear: (x: Int)Boolean
结果:
注意:在定义函数的时候,可以不写返回值类型,Scala会帮咋们自动推导对应的数据类型
scala> isLeapYear(1600)
res11: Boolean = true
scala> isLeapYear(2000)
res12: Boolean = true
scala> isLeapYear(2019)
res13: Boolean = false
scala> isLeapYear(2020)
res14: Boolean = true
scala> isLeapYear(2004)
res15: Boolean = true
```
六.循环语句
package day1018
import scala.math.sqrt
import util.control.Breaks._
/**
* 作者:Jackson
* Created by root on 2019/10/18
* scala循环
*
* object相当于 java中的static(静态的)
*
* java中是使用static关键字,表示静态
*
* Object中所有的内容都是静态的
*/
object Demo1 {
def main(args: Array[String]): Unit = {
//for循环
//定义一个集合
println("----------for循环第一种写法-----------")
var list = List("Jack", "Tom", "andy", "plus")
for (s <- list) println(s)
//<-表示scala中的提取符,把list中的每一个元素提取出来赋值给s
println("----------for循环第二种写法-----------")
for {
s <- list
if (s.length > 3)
} println(s)
println("----------for循环第三种写法-----------")
for (s <- list if s.length <= 3) println(s)
//使用yield关键字 作用:产生一个新的集合
println("----------for循环第四种写法-----------")
var newList = for {
s <- list
s1 = s.toUpperCase() //把名字变成大写
} yield (s1)
for (s1 <- newList) println(s1)
println("----------while循环写法-----------")
//定义循环变量
var i = 0
while (i < list.length) {
println(list(i))
i += 1
}
println("----------do while循环写法-----------")
//定义循环变量
var j = 0
do {
println(list(j))
j += 1
} while (j < list.length)
println("----------foreach循环写法-----------")
list.foreach(println)
//foreach函数,就是把每个元素取出来赋值给参数的函数->println 打印出来list中每个元素
//foreach以后用的非常多
/**
* 判断101-200之间有多少个素数
*
* 程序分析实现方法:
* 判断一个素数的方法:
* 用一个数分别去除2到sprt(这个数),如果可以被整除,则表明此数不是素数,反之是素数
*
* 程序实现方法:
* 定义俩层循环
* 第一层:101到200
* 第二层:2->sqrt(第一层循环的数)
*
* 举例:16
* 2循环到sqrt(16)4
**/
print("--------循环嵌套---------")
var index_inner = 0
var count = 0
for (index_outner <- 101 until 200) {
var b = false
breakable {
index_inner = 2
while (index_inner <= sqrt(index_outner)) {
if (index_outner % index_inner == 0) {
//是素数
b = true
break
}
index_inner += 1
}
}
if (!b) count += 1
}
println("个数为:" + count)
}
}
————保持饥饿,保持学习
Jackson_MVP