在scala中实现打印从10到3:
1.使用breakable-break的方式跳出循环
导入的类是:import scala.util.control.Breaks._
object demo2
{
def main
(args
:Array
[String]):Unit = {
import scala
.util
.control
.Breaks
._
var num
=10
breakable
{
for(x
<- num to
1 by
-1 if x
>=3 ) print
(x
+"\t")
}
}
}
2.使用loop.breakable-loop.break的方式跳出循环
导入的类是:import scala.util.control.Breaks
object demo2
{
def main
(args
:Array
[String]):Unit = {
import scala
.util
.control
.Breaks
val num
=10
val loop
= new Breaks
loop
.breakable
(
for(x
<- num to
3 by
-1){
if(x
<3) loop
.break
print
(x
+"\t")
}
)
}
}
使用变量接收循环语句的结果集合(yield关键字),遍历集合
object demo2
{
def main
(args
:Array
[String]):Unit = {
var i
=100;
val resValue
=for(x
<-1 to i
; if x
%2==0;if x
%5==0) yield x
println
(resValue
)
println
("-------------------")
for(i
<- resValue
){
print
(i
+"\t")
}
}
}
结果显示:
Vector(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
-------------------
10 20 30 40 50 60 70 80 90 100
Process finished with exit code 0
if流程控制:
判断i的值和10的大小关系,打印
object demo2
{
def main
(args
:Array
[String]):Unit = {
val i
=10
if(i
>10){
println
(i
+"的值大于10")
}else if(i
==10){
println
(i
+"的值等于10")
}else{
println
(i
+"的值小于10")
}
}
}
练习:判断i的值和10是否相等,相等加1,不相等加2,并打印
object demo2
{
def main
(args
:Array
[String]):Unit = {
val i
=10
val j
=if(i
==10) i
+1 else i
+2
println
(j
)
}
}
定义代码块:
代码块的结果是在代码块中计算的最后一行
object demo2
{
def main
(args
:Array
[String]):Unit = {
val x
=10
val y
={
print
("代码块\t")
x
+1
}
println
(y
)
println
("---------------")
val y2
={
x
+1;println
("代码块")
}
println
(y2
)
}
}
打印结果:
代码块 11
---------------
代码块
()
Process finished with exit code 0
循环语句
在for循环中: 1 to 10 可以取到10 1 until 10 取不到10
object demo2
{
def main
(args
:Array
[String]):Unit = {
var x
=0
println
("-------while循环------")
while(x
<10){
print
(x
+"\t")
x
+=1
}
x
=0
println
("\n-------do-while循环------")
do{
print
(x
+"\t")
x
+=1
}while(x
<10)
println
("\n-------for循环(1 to 10)------")
for(x
<- 1 to
10) print
(x
+"\t")
println
("\n-------for循环(1 until 10)------")
for(x
<- 1 until
10) print
(x
+"\t")
}
}
打印结果:
-------while循环------
0 1 2 3 4 5 6 7 8 9
-------do-while循环------
0 1 2 3 4 5 6 7 8 9
-------for循环(1 to 10)------
1 2 3 4 5 6 7 8 9 10
-------for循环(1 until 10)------
1 2 3 4 5 6 7 8 9
Process finished with exit code 0
练习:使用循环打印九九乘法口诀表
外层循环速度慢,内层循环速度快
object demo2
{
def main
(args
:Array
[String]):Unit = {
for(x
<-1 until
10){
for (y
<-1 to x
){
print
(s
"$x*$y=${x*y}\t")
}
println
()
}
}
}
打印结果:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
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
使用循环打印等腰三角形:
object demo2
{
def main
(args
:Array
[String]):Unit = {
val num
=5
for(i
<-1 to num
){
for (j
<-num to i by
-1){
print
(" ")
}
for(k
<-1 to
2*i
-1) print
("*")
println
()
}
}
}
显示结果:
*
***
*****
*******
*********
Process finished with exit code 0
练习:打印一个中间掏空的等腰三角形
在原来等腰三角形的基础上,第一行和最后一行要正常打印,其余每一行只打印第一个*和最后一个*其余打印空格
object demo2
{
def main
(args
:Array
[String]):Unit = {
val num
=5
for(i
<-1 to num
){
for (j
<-num to i by
-1){
print
(" ")
}
for(k
<-1 to
2*i
-1){
if(i
==1 || i
==num
|| k
==1 ||k
==2*i
-1) print
("*") else print
(" ")
}
println
()
}
}
}
打印结果:
*
* *
* *
* *
*********
Process finished with exit code 0
元组
与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素。
元组的值是通过将单个的值包含在圆括号中构成的。
object demo3
{
def main
(args
:Array
[String]):Unit ={
var tupleDemo
=("hello","kb09",1.2,3,"world","a")
println
(tupleDemo
)
println
("tupleDemo._1的值:"+tupleDemo
._1
)
println
("tupleDemo._2的值:"+tupleDemo
._2
)
println
("tupleDemo._3的值:"+tupleDemo
._3
)
println
("tupleDemo._4的值:"+tupleDemo
._4
)
println
("tupleDemo._5的值:"+tupleDemo
._5
)
println
("tupleDemo._6的值:"+tupleDemo
._6
)
}
}
元组的遍历方式
tupleDemo
.productIterator
.foreach
(println
)
tupleDemo
.productIterator
.foreach
(x
=> println
(s
"value:$x"))
显示结果:
value:hello
value:kb09
value:1.2
value:3
value:world
value:a
元组的定义方式
var tupleDemo2
= new Tuple5
("ab","bc","cd","de","ef")
或者:
object demo3
{
def main
(args
:Array
[String]):Unit ={
def mike
="mike"->5->6
println
(mike
._1
)
println
(mike
._2
)
println
(mike
.getClass
)
}
}
显示结果: mike的类型是:Tuple2 ((String,Int),Int)
(mike,5)
6
class scala.Tuple2
Process finished with exit code 0
object demo3
{
def main
(args
:Array
[String]):Unit ={
val tp1
=("zhangsan","nanjing",19)
val(name
,address
,age
)=tp1
println
(s
"姓名:$name,地址:$address,年龄:$age")
}
}
显示结果:
姓名:zhangsan,地址:nanjing,年龄:19
数组的定义
object demoArray
{
def main
(args
:Array
[String]):Unit={
var arr
:Array
[String]=new Array
[String](3)
arr
(0)="hello"
arr
(1)="world"
arr
(2)="scala"
println
(arr
(0))
println
("--------arr2--------")
var arr2
=Array
("kb09","luoxin","gree")
println
(arr2
(1))
}
}
显示结果:
hello
--------arr2--------
luoxin
函数的定义和使用
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def minValue
(a
:Int,b
:Int):Int=if(a
>b
) b
else a
def sumValue
(a
:Int,b
:Int,c
:String):Int=if(c
.equals
("+")) a
+b
else a
-b
}
}
使用递归的方式写出阶乘
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def jiecheng
(a
:Int):Int={
if(a
==1){
a
}else{
a
*jiecheng
(a
-1)
}
}
println
(jiecheng
(8))
}
}
通过指定参数名字,传递参数值
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def showMsg
(name
:String,age
:Int):Unit={
println
(s
"hello:$name,age:$age")
}
showMsg
("zhangsan",18)
showMsg
(age
=18,name
="zhangsan")
}
}
参数的长度未知,通过*来表示,可以传入多个参数
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def showMsg2
(name
:String,s
:String*)={
println
(name
)
for(str
<-s
){
println
(str
)
}
}
showMsg2
("kb09","abc","hello")
}
}
匿名函数
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def sum
(a
:Int,b
:Int):Int=a
+b
val aa
= (a
:Int,b
:Int) => a
+b
println
("aa(32,31)的结果为"+aa
(32,31))
val bb
=aa
println
("bb(66,88)的结果为"+bb
(66,88))
val cc
=(a
:Int,b
:Int)=>"abc"
println
("cc(11,22)的结果为"+cc
(12,21))
val sum1
:(Int,Int)=>Int = (a
:Int,b
:Int)=>a
+b
val sum2
:(Int,Int)=>Int = aa
println
("sum1(1,3)的结果为"+sum1
(1,3))
println
("sum2(12,23)的结果为"+sum2
(12,23))
}
}
显示结果:
aa(32,31)的结果为63
bb(66,88)的结果为154
cc(11,22)的结果为abc
sum1(1,3)的结果为4
sum2(12,23)的结果为35
函数作为参数进行传递
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def fun
(a
:Int,b
:Int):Int =a
+b
def fun1
(a
:Int,b
:Int):Int=a
-b
def fun2
(a
:Int,b
:Int):Int=a
*b
def funTest
(f
:(Int,Int)=>Int,s
:String):String={
val resultValue
=f
(1000,3000)
s
+resultValue
}
var resultValue
=funTest
(fun
,"你的薪水是:")
println
(resultValue
)
println
("-----使用匿名函数作为参数传递-----")
resultValue
=funTest
((a
:Int,b
:Int)=>a
+b
,"您的薪水是:")
println
(resultValue
)
}
}
显示结果:
你的薪水是:4000
-----使用匿名函数作为参数传递-----
您的薪水是:4000
函数作为返回值
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def funTest2
():(String,String)=>String={
def fun2
(str1
:String,str2
:String):String={
str1
+"##"+str2
+"&&"
}
fun2
}
println
(funTest2
()("aa", "bb"))
}
}
显示结果:
aa##bb&&
练习:先传递一个字符串,如果长度大于5,传递两个整数相加,如果字符串长度小于5,传递两个整数相减,如果字符串长度等于5,传递两个整数相乘
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def funTest2
(s
:String):(Int,Int)=>Int={
if(s
.length
>5){
def funAdd
(a
:Int,b
:Int):Int= a
+b
funAdd
}else if(s
.length
<5){
def funMinus
(a
:Int,b
:Int):Int=a
-b
funMinus
}else{
def funMultiply
(a
:Int,b
:Int):Int=a
*b
funMultiply
}
}
val intValue
= funTest2
("Hello")(100,200)
println
(intValue
)
}
}
练习:传入一个数字,如果能被3整除,返回加法,否则返回减法
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def getFunByNum
(num
:Int):(Int,Int)=>Int ={
if(num
%3==0){
(a
:Int,b
:Int)=>a
+b
}else{
(a
:Int,b
:Int)=>a
-b
}
}
println
(getFunByNum
(4)(100, 200))
}
}
函数作为参数传递进来,函数作为返回值返回出去
相同需求改进练习:传入一个数字,如果能被3整除,返回加法,否则返回减法
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def funMsg
(f
:(String)=>Int,str
:String):(Int,Int)=>Int ={
val intNum
=f
(str
)
if(intNum
%3==0){
(a
:Int,b
:Int)=>a
+b
}else{
(a
:Int,b
:Int)=>a
-b
}
}
val resultNum
= funMsg
(
(a
: String) => {if (a
.equals
("上海")) 3 else 1}, "上海")(100, 500)
println
(resultNum
)
}
}
练习:先传入一个数字,判断是否能被3整除,再传入两个整数,进行两数相加/相减,判断结果值是否大于3000,再传递字符串,打印不同的打印语句
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def funTest3
(a
:Int):(Int,Int)=>(String)=>Unit ={
if(a
%3==0){
def funAdd
(a
:Int,b
:Int):(String)=>Unit ={
if(a
+b
>3000){
(x
:String) => {
println
("恭喜"+x
+"你的收入很高")
}
}else{
(x
:String) => {
println
("很遗憾"+x
+"再加把力")
}
}
}
funAdd
}else{
def funMinus
(a
:Int,b
:Int):(String)=>Unit ={
if(a
-b
>3000){
(x
:String)=>{
println
(x
+"你是一个非常节约的人")
}
}else{
(x
:String)=>{
println
(x
+"????你已经入不敷出了")
}
}
}
funMinus
}
}
funTest3
(7)(5000,7000)("zhangsan")
}
}
函数柯理化
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
def fun
(a
:Int,b
:Int,c
:Int,d
:Int):Int={a
+b
+c
+d
}
def fun1
(a
:Int,b
:Int)(c
:Int,d
:Int):Int={a
+b
+c
+d
}
def fun2
(a
:Int)(b
:Int)(c
:Int)(d
:Int):Int={a
+b
+c
+d
}
def fun3
(a
:Int,b
:Int,c
:Int)(d
:Int):Int={a
+b
+c
+d
}
def fun4
(a
:Int)(b
:Int,c
:Int,d
:Int):Int={a
+b
+c
+d
}
println
(fun
(1, 2, 3, 4))
println
(fun1
(1, 2)(3, 4))
println
(fun2
(1)(2)(3)(4))
println
(fun3
(1, 2, 3)(4))
println
(fun4
(1)(2, 3, 4))
}
}
隐式参数
object FunctionDemo
{
def main
(args
:Array
[String]):Unit={
implicit var a
:Int=10
implicit var str
:String="hello"
def fun5
(a
:Int,b
:Int)(c
:Int=5):Int={a
+b
+c
}
def fun6
(a
:Int,b
:Int)(implicit c
:Int=5):Int={a
+b
+c
}
def fun7
(a
:Int,b
:Int)(implicit c
:Int=5,str
:String):Int={
println
(str
)
a
+b
+c
}
println
("fun5(10, 10)()的结果是:"+fun5
(10, 10)())
println
("fun6(10,10)(20)的结果:"+fun6
(10,10)(20))
println
("fun6(10, 10)的结果:"+fun6
(10, 10))
println
("fun6(10,10)()的结果"+fun6
(10,10)())
println
("fun7(10,10)的结果"+fun7
(10,10))
println
("fun7(10,10)(c=123,str=\"abc\")的结果是:"+fun7
(10,10)(c
=123,str
="abc"))
}
}
显示结果:
fun5(10, 10)()的结果是:25
fun6(10,10)(20)的结果:40
fun6(10, 10)的结果:30
fun6(10,10)()的结果25
hello
fun7(10,10)的结果30
abc
fun7(10,10)(c=123,str="abc")的结果是:143
模式匹配和样例类
object demoMatch
{
def method1
(x
:Int):String={
if(x
==1){
"one"
}else if(x
==2){
"two"
}else{
"many"
}
}
def match1
(x
:Int):String=x
match{
case 1 => "one"
case 2 => "two"
case _
=> "many"
}
def match2
(x
:Any):Unit=x
match{
case 1 => println
("输入的值是1")
case i
:Int => println
("输入的值类型为Int类型,值为"+i
)
case s
:Int => println
("输入的值类型为String类型,值为"+s
)
case _
=> println
("我不知道是啥")
}
case class Teacher
(name
:String,age
:Int)
def match3
(teacher
:Teacher
):Unit=teacher
match{
case Teacher
("gree",19) => println
("hello gree")
case Teacher
("wenwen",21) => println
("hello wenwen")
case x
:Teacher
=>{println
("hello"+x
.name
)}
}
def main
(args
: Array
[String]): Unit = {
println
(match1
(2))
match2
(1)
val t
=Teacher
("gree",19)
val t1
=Teacher
("gree",19)
if(t
==t1
){
println
("是同一个人")
}else{
println
("不是同一个人")
}
match3
(t
)
}
}
显示结果:
two
输入的值是1
是同一个人
hello gree