赞
踩
- 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_~
- 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}
- scala> s"My name is Tom and ${s1}"
- res7: String = My name is Tom and Hello world
- scala> val b : Int = 11
- b: Int = 11
- scala> b = 12
- <console>:12: error: reassignment to val
- b = 12
- ^
- 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> 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
- ^
- scala> val f = ()
- f: Unit = ()
- scala> def myFunction = throw new Exception("Some Error")
- myFunction: Nothing
-
- scala> def myexception=throw new Exception("yufa exception")
- myexception: Nothing
- scala> max(1,2)
- <console>:12: error: not found: value max
- max(1,2)
- ^
- scala> import scala.math._
- import scala.math._
- scala> max(1,2)
- res9: Int = 2
res9: Int = 2
- 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
- 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
- scala> def myFactor(x:Int) : Int = {
- | if (x <= 1)
- | 1
- | else
- | x*myFactor(x-1)
- | }
- myFactor: (x: Int)Int
- scala> myFactor(5)
- res11: Int = 120
-
- 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。