feat:线下课:流程控制、方法和函数
ChatGPT:第 2 课:Scala 函数与方法
This commit is contained in:
@@ -46,7 +46,7 @@ object DemoOps {
|
||||
|
||||
a &= 1 // a = a & 1 // 按位与 运算
|
||||
a |= 1 // a = a | 1 // 按位或 运算
|
||||
a ^= 1 // a = a ^ 1 // 异或 运算
|
||||
a ^= 1 // a = a ^ 1 // 异或 运算
|
||||
|
||||
|
||||
val b = 10
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package chapter1.demo4
|
||||
|
||||
import scala.util.control.Breaks.{break, breakable}
|
||||
|
||||
object DemoProcessControl {
|
||||
def main(args: Array[String]): Unit = {
|
||||
ifelse()
|
||||
// whileLoop()
|
||||
// forLoop()
|
||||
// continueMethod()
|
||||
// breakMethod()
|
||||
}
|
||||
|
||||
def ifelse(): Unit={
|
||||
val age = 19
|
||||
if(age >= 18){
|
||||
println("成年了")
|
||||
}else{
|
||||
println("未成年")
|
||||
}
|
||||
if(age <=6){
|
||||
println("童年")
|
||||
}else if (age < 18){
|
||||
println("青少年")
|
||||
}else if (age < 40){
|
||||
println("青年/壮年")
|
||||
}else{
|
||||
println("其他")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def whileLoop(): Unit = {
|
||||
var i = 1
|
||||
println("循环开始")
|
||||
while (i < 3) {
|
||||
println(s"i = ${i}")
|
||||
i += 1
|
||||
}
|
||||
println("循环结束了")
|
||||
|
||||
val b = 0
|
||||
// do部分代码,至少会执行一次
|
||||
do {
|
||||
println(s"b=${b}")
|
||||
} while (b > 0)
|
||||
|
||||
|
||||
}
|
||||
|
||||
def forLoop(): Unit = {
|
||||
// 1. 范围遍历
|
||||
println("==== until ====")
|
||||
for (i <- 0 until 3) {
|
||||
println(s"i = ${i}")
|
||||
}
|
||||
|
||||
println("==== to ====")
|
||||
for (i <- 0 to 3) {
|
||||
println(s"i = ${i}")
|
||||
}
|
||||
|
||||
// 2. 集合遍历
|
||||
println("==== List ====")
|
||||
for (i <- List(1, 2, 3)) {
|
||||
println(s"list data: ${i}")
|
||||
}
|
||||
println("==== Array ====")
|
||||
for (i <- Array(1, 2, 3)) {
|
||||
println(s"Array data: ${i}")
|
||||
}
|
||||
|
||||
|
||||
// 3. 循环守卫
|
||||
println("==== 循环守卫 ====")
|
||||
// for(i <- 0 until 10) {
|
||||
// if(i!=5){
|
||||
// println(i)
|
||||
// }
|
||||
// }
|
||||
for (i <- 0 until 10 if i != 5) {
|
||||
println(i)
|
||||
}
|
||||
|
||||
// 4. 循环步长
|
||||
|
||||
println("==== 循环步长 ====")
|
||||
for (i <- 0 until 10 by 2) {
|
||||
println(i)
|
||||
}
|
||||
|
||||
|
||||
println("==== 循环步长-2 ====")
|
||||
for (i <- 10 until 0 by -2) {
|
||||
println(i)
|
||||
}
|
||||
|
||||
// 5. 循环嵌套
|
||||
println("==== 循环嵌套 ====")
|
||||
for (i <- 1 to 3) {
|
||||
for (j <- 1 to 3) {
|
||||
println(s"i = ${i},j = ${j}")
|
||||
}
|
||||
}
|
||||
|
||||
// 灵活的用法:
|
||||
|
||||
println("==== 循环嵌套2 ====")
|
||||
for (i <- 1 to 3; j <- 1 to 3) {
|
||||
println(s"i = ${i},j = ${j}")
|
||||
|
||||
}
|
||||
|
||||
// 6. 循环中引入变量
|
||||
|
||||
println("==== 循环中引入变量 ====")
|
||||
for (i <- 0 until 3) {
|
||||
val j = 3 - i
|
||||
println(s"i = ${i},j = ${j}")
|
||||
}
|
||||
|
||||
|
||||
println("==== 循环中引入变量2 ====")
|
||||
for (i <- 0 until 3; j = 3 - i) {
|
||||
println(s"i = ${i},j = ${j}")
|
||||
}
|
||||
|
||||
println("==== 循环中引入变量3 ====")
|
||||
for {
|
||||
i <- 0 until 3
|
||||
j = 3 - i
|
||||
} {
|
||||
println(s"i = ${i},j = ${j}")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
def continueMethod(): Unit = {
|
||||
println("continue")
|
||||
for (i <- 1 to 10) {
|
||||
breakable {
|
||||
if (i == 5) {
|
||||
println("跳过5")
|
||||
break
|
||||
}
|
||||
println(s"i = $i")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def breakMethod(): Unit = {
|
||||
println("break")
|
||||
breakable {
|
||||
for (i <- 1 to 10) {
|
||||
if (i == 5) {
|
||||
println("跳过5")
|
||||
break
|
||||
}
|
||||
println(s"i = $i")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package chapter1.exercises
|
||||
|
||||
import scala.io.StdIn
|
||||
|
||||
object LeapYearDetermination {
|
||||
def main(args: Array[String]): Unit = {
|
||||
print("请输入年份:")
|
||||
val year = StdIn.readInt()
|
||||
println(s"输入的年份是 ${year}")
|
||||
println(ifYear(year))
|
||||
|
||||
def ifYear(year:Int): String ={
|
||||
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
|
||||
"闰年"
|
||||
}else{
|
||||
"不是闰年"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package chapter1.exercises
|
||||
|
||||
object MultiplicationTable {
|
||||
def main(args: Array[String]): Unit = {
|
||||
|
||||
for (i <- 1 to 9) {
|
||||
for (j <- 1 to i) {
|
||||
print(s"$j * $i = ${i * j} ")
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
for (i <- 1 to 9; j <- 1 to i) {
|
||||
print(s"$j * $i = ${i * j} ")
|
||||
|
||||
if (j == i) {
|
||||
println()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package chapter2.demo1
|
||||
|
||||
object DemoMethod {
|
||||
def main(args: Array[String]): Unit = {
|
||||
|
||||
// 函数
|
||||
def sayHello(name: String): Unit = {
|
||||
println(s"hello,${name}")
|
||||
}
|
||||
|
||||
sayHello("zhangsan") // hello
|
||||
// 通过类名去调用方法,等同于java中的静态方法。要求方法定义在object中
|
||||
DemoMethod.sayHello("zhangsan") // hello in object
|
||||
// 创建对象,然后再去调用对象的 sayHello 方法,等同于java中的普通方法。
|
||||
val obj: DemoMethod = new DemoMethod()
|
||||
obj.sayHello("zhangsan") // hello in class
|
||||
|
||||
}
|
||||
|
||||
// 伴生对象中的方法,等同于java的静态方法
|
||||
def sayHello(name: String): Unit = {
|
||||
println(s"hello in object,${name}")
|
||||
}
|
||||
}
|
||||
|
||||
class DemoMethod{
|
||||
|
||||
// 普通方法,不是静态方法
|
||||
def sayHello(name: String): Unit = {
|
||||
println(s"hello in class,${name}")
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package practise1.overall
|
||||
package practise1.comprehensive
|
||||
|
||||
object Overall {
|
||||
object Comprehensive {
|
||||
def main(args: Array[String]): Unit = {
|
||||
/*第 1 课综合题
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package practise1.practise
|
||||
package practise1.exercises
|
||||
|
||||
object Practise1 {
|
||||
object Exercises1 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
/* 练习 1:基本变量
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package practise1.practise
|
||||
package practise1.exercises
|
||||
|
||||
object Practise2 {
|
||||
object Exercises2 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
/* 练习 2:判断年龄
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package practise1.practise
|
||||
package practise1.exercises
|
||||
|
||||
object Practise3 {
|
||||
object Exercises3 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
/* 练习 3:计算两个数
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
package practise1.practise
|
||||
package practise1.exercises
|
||||
|
||||
import scala.util.Random
|
||||
|
||||
object Practise4 {
|
||||
object Exercises4 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
/*练习 4:成绩判断
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package practise2.comprehensive
|
||||
/*
|
||||
练习 6:综合题 ⭐
|
||||
|
||||
写一个函数:
|
||||
describeStudent(name, age, score)
|
||||
|
||||
例如:
|
||||
describeStudent("Tom", 22, 85)
|
||||
|
||||
输出:
|
||||
Tom,今年22岁
|
||||
成绩:85
|
||||
评价:良好
|
||||
|
||||
要求:
|
||||
name 是 String
|
||||
age 是 Int
|
||||
score 是 Int
|
||||
函数返回 String
|
||||
评价部分使用 if
|
||||
不要在函数里面直接 println
|
||||
*/
|
||||
object comprehensive {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val result = describeStudent("Tom", 22, 85)
|
||||
println(result)
|
||||
}
|
||||
|
||||
def describeStudent(name: String, age: Int, score: Int): String ={
|
||||
val evaluate = if (score >= 90) {
|
||||
"优秀"
|
||||
} else if (score >= 80) {
|
||||
"良好"
|
||||
} else if (score >= 60) {
|
||||
"及格"
|
||||
} else {
|
||||
"不及格"
|
||||
}
|
||||
|
||||
s"""
|
||||
|$name,今年${age}岁
|
||||
|成绩:$score
|
||||
|评价:${evaluate}""".stripMargin
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package practise2.demo
|
||||
|
||||
object DemoFunction {
|
||||
def main(args: Array[String]): Unit = {
|
||||
hello()
|
||||
// 简单的函数
|
||||
def hello(): Unit = {
|
||||
println("Hello Scala")
|
||||
}
|
||||
|
||||
println("========")
|
||||
|
||||
// 有参数的函数,定义一个叫 add 的函数,接收两个 Int 参数,返回一个 Int。
|
||||
/*
|
||||
def 函数名(参数名: 参数类型): 返回值类型 = {
|
||||
函数体
|
||||
}
|
||||
*/
|
||||
def add(a: Int, b: Int): Int = {
|
||||
a + b // Scala 不需要显式 return
|
||||
}
|
||||
val result = add(10, 20)
|
||||
println(result)
|
||||
|
||||
println("========")
|
||||
|
||||
// 最后一句是什么,就返回什么
|
||||
def test(): String = {
|
||||
val a = 10
|
||||
println(a) // 只是打印
|
||||
"Hello" // 才是返回值
|
||||
}
|
||||
val resulTest = test()
|
||||
|
||||
println("========")
|
||||
|
||||
// 无参数函数
|
||||
def hello2(): String = {
|
||||
"Hello"
|
||||
}
|
||||
println(hello2())
|
||||
println(hello2) // 也可以省略 ()
|
||||
|
||||
|
||||
// 函数可以非常短
|
||||
def add2(a: Int, b: Int): Int = a + b // 等价add函数
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package practise2.exercises
|
||||
/*
|
||||
练习 1:加法函数
|
||||
|
||||
定义:
|
||||
add(a, b)
|
||||
|
||||
要求:
|
||||
输入 10 和 20
|
||||
返回 30
|
||||
|
||||
调用:
|
||||
println(add(10, 20))
|
||||
*/
|
||||
|
||||
object Exercises1 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
print(add(10,20))
|
||||
}
|
||||
def add(a: Int, b: Int): Int = a + b
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package practise2.exercises
|
||||
/*
|
||||
练习 2:计算平方
|
||||
|
||||
定义:
|
||||
square(x)
|
||||
|
||||
例如:
|
||||
println(square(5))
|
||||
|
||||
输出:
|
||||
25
|
||||
要求返回类型写出来。
|
||||
*/
|
||||
|
||||
object Exercises2 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
print(square(5))
|
||||
}
|
||||
def square(x: Int): Int = x * x
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package practise2.exercises
|
||||
/*
|
||||
练习 3:判断成年人
|
||||
定义:
|
||||
isAdult(age)
|
||||
例如:
|
||||
18 → true
|
||||
20 → true
|
||||
17 → false
|
||||
要求返回:
|
||||
Boolean
|
||||
*/
|
||||
|
||||
object Exercises3 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
print(isAdult(20))
|
||||
}
|
||||
def isAdult(age: Int): Boolean = age >= 18
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package practise2.exercises
|
||||
|
||||
/*
|
||||
练习 4:成绩评价
|
||||
定义:
|
||||
getLevel(score)
|
||||
要求:
|
||||
90以上 → 优秀
|
||||
80以上 → 良好
|
||||
60以上 → 及格
|
||||
否则 → 不及格
|
||||
例如:
|
||||
println(getLevel(95))
|
||||
println(getLevel(83))
|
||||
println(getLevel(61))
|
||||
println(getLevel(30))
|
||||
输出:
|
||||
优秀
|
||||
良好
|
||||
及格
|
||||
不及格
|
||||
*/
|
||||
|
||||
object Exercises4 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
println(getLevel(95))
|
||||
println(getLevel(83))
|
||||
println(getLevel(61))
|
||||
println(getLevel(30))
|
||||
}
|
||||
|
||||
def getLevel(score: Double): String =
|
||||
if (score >= 90) {
|
||||
"优秀"
|
||||
} else if (score >= 80) {
|
||||
"良好"
|
||||
} else if (score >= 60) {
|
||||
"及格"
|
||||
} else {
|
||||
"不及格"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package practise2.exercises
|
||||
|
||||
/*
|
||||
练习 5:计算两个数中的较大值
|
||||
定义:
|
||||
max(a, b)
|
||||
例如:
|
||||
println(max(10, 20))
|
||||
输出:
|
||||
20
|
||||
要求返回较大的那个数。
|
||||
*/
|
||||
object Exercises5 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
println(max(10, 20))
|
||||
}
|
||||
|
||||
def max(a: Int, b: Int): Int =
|
||||
if (a > b) a else b
|
||||
}
|
||||
Reference in New Issue
Block a user