线下课:
1、变量和数据类型
2、运算符

ChatGPT Scala 第 1 课:
变量、类型、条件判断
This commit is contained in:
2026-09-09 17:28:36 +08:00
commit f27e286f7e
15 changed files with 429 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
+10
View File
@@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# 已忽略包含查询文件的默认文件夹
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>scala_20260908</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
+32
View File
@@ -0,0 +1,32 @@
package demo1
object DemoVar {
def main(args: Array[String]): Unit = {
// 定义一个变量a,因为是用val定义,所以其实是个常量
val a = 10
// 常量不可以再修改值,所以x = 11会报错
// x = 11
//定义一个变量y,因为用var定义,所以是个真的变量
var b = 10
b = 11
println(b)
println("============================================================")
case class people(var high: Int, var weight: Int)
val xiaoMing = new people(170, 110)
// xiaoMing = new people(173, 110)
// ❌ 因为 xiaoMing 是 val,不能让它重新指向另一个对象
xiaoMing.high = 173
println(xiaoMing)
println(xiaoMing.weight)
}
}
+29
View File
@@ -0,0 +1,29 @@
package demo2
object DemoStr {
def main(args: Array[String]): Unit = {
val name: String = "zhangsan"
val age: Int = 18
println(s"name=${name},age=${age}")
println(name * 3)
val duohang: String =
"""
|123
|456
|789
|""".stripMargin
println(s"duohang = ${duohang}")
val s1: String = "hello"
val s2: String = new String("hello")
println(s1 == s2) // true scala中的==其实等于java中的equals
println(s1 != s2)
println(s1.equals(s2)) //true 其实等于java中的equals其实等于java中的equals
println(s1.eq(s2)) // false scala中的eq就是java中的==,比较两个对象的内存地址
}
}
+48
View File
@@ -0,0 +1,48 @@
package demo2
object DemoVar2 {
def main(args: Array[String]): Unit = {
val a1: Byte = 10
val b1: Long = 1234
println(s"a1的数据类型是 ${a1.getClass.getSimpleName}")
println(s"b1的数据类型是 ${b1.getClass.getSimpleName}")
// 隐式转换 a1自动转成long类型与b1相加
val result1 = a1 + b1
println(s"result1的数据类型是 ${result1.getClass.getSimpleName}")
println(s"result1 = ${result1}")
val a2: Byte = 10
val b2: Int = a2
//报错 无法隐式转换(大变小)
// val c2:Byte = b2
val c2 = b2.toByte
val b3: Int = 129
val c3: Byte = b3.toByte
// c3 会等于129吗? 数值区间为 -128 到 127
println(s"c3 = ${c3}")
val a4: Byte = 10
val b4: Char = 'b'
// type mismatch; 虽然编译通过,但是运行报错
// val c4: Byte = 64
// println(s"c4=${c4}")
val a5: Int = 97
// Char隐式转换为Int
println(s"b4+a5 = ${b4 + a5}")
val b5: Char = a5.toChar
println(s"b5 = ${b5}")
val a6: Int = 10
val b6: Float = a6
val c6: Int = b6.toInt
println(s"b6 = ${b6}")
println(s"c6 = ${c6}")
}
}
@@ -0,0 +1,22 @@
package practise1
object Demo1VarAndVal {
def main(args: Array[String]): Unit = {
//1. val 和 var
val name = "Kenneth"
// name = "Tom" // ❌ 不允许修改
var age = 25
age = 26 // ✅ 可以修改
println(name)
println(age)
println("=================================")
println("=================================")
}
}
+37
View File
@@ -0,0 +1,37 @@
package practise1
object Demo2Type {
def main(args: Array[String]): Unit = {
//2. 类型
//
//Scala 支持自动推断类型:
val name = "xiaoMing" // String
val age = 25 // Int
val price = 19.99 // Double
val flag = true // Boolean
// 也可以明确指定:
val name2: String = "xiaoMing"
val age2: Int = 25
val price2: Double = 19.99
//Scala 和 Java 很像,但 Scala 更倾向于让编译器帮你推断类型。
println(name.getClass)
println(age.getClass)
println(price.getClass)
println(flag.getClass)
println(name2.getClass)
println(age2.getClass)
println(price2.getClass)
println("===================================")
println(name.getBytes().mkString(","))
println("===================================")
println(age.isInstanceOf[Int])
println(age.getClass)
println("===================================")
println(name.isInstanceOf[String])
println(name.getClass)
}
}
@@ -0,0 +1,16 @@
package practise1
object Demo3StringInterpolation {
// 这是 Scala 很常用的语法:
val name = "xiaoMing"
val age = 25
println(s"我的名字是 $name,今年 $age 岁")
// 表达式需要使用 {}
val a = 10
val b = 20
println(s"$a + $b = ${a + b}")
}
+27
View File
@@ -0,0 +1,27 @@
package practise1
object Demo4If {
def main(args: Array[String]): Unit = {
// Scala 的 if 和 Java 类似
val age = 20
if(age >=18){
println("成年人")
}else{
println("未成年")
}
// 但是 Scala 有一个非常重要的特点:if 本身就是一个表达式,可以返回值
val result = if(age >= 18){
"成年人"
}else{
"未成年人"
}
println(result)
}
}
+34
View File
@@ -0,0 +1,34 @@
package practise1
object Overall {
def main(args: Array[String]): Unit = {
/*第 1 课综合题
写一个 Scala 程序:
姓名:Tom
年龄:22
成绩:85
最终输出:
Tom,今年22岁
成绩:85
评价:良好*/
val name = "Tom"
val age = 22
val score = 85
val evaluate =
if (score >= 90 ) "优秀"
else if (score >= 80) "良好"
else if (score >= 60) "及格"
else "不及格"
println(
s"""
|${name},今年${age}岁
|成绩:${score}
|评价:${evaluate}""".stripMargin)
}
}
+21
View File
@@ -0,0 +1,21 @@
package practise1
object Practise1 {
def main(args: Array[String]): Unit = {
/* 练习 1:基本变量
创建三个变量:
name = "Tom"
age = 20
salary = 15000.5
要求:
输出:Tom今年20岁,工资是15000.5*/
val name = "Tom"
val age = 20
val salary = 15000.5
println(s"${name}今年${age}岁,工资是${salary}")
}
}
+24
View File
@@ -0,0 +1,24 @@
package practise1
object Practise2 {
def main(args: Array[String]): Unit = {
/* 练习 2:判断年龄
定义:
val age = 22
判断:
18岁及以上:成年人
18岁以下:未成年人
要求使用 if...else。*/
val age = 22
if (age >= 18) {
print("成年人")
} else {
println("未成年人")
}
}
}
+26
View File
@@ -0,0 +1,26 @@
package practise1
object Practise3 {
def main(args: Array[String]): Unit = {
/* 练习 3:计算两个数
定义:
val a = 10
val b = 20
输出:
10 + 20 = 30
10 - 20 = -10
10 * 20 = 200
10 / 20 = 0.5*/
val a = 10
val b = 20
println(s"${a}+${b} = ${a + b}")
println(s"${a}-${b} = ${a - b}")
println(s"${a}*${b} = ${a * b}")
println(s"${a}/${b} = ${a.toDouble / b}")
}
}
+47
View File
@@ -0,0 +1,47 @@
package practise1
import scala.util.Random
object Practise4 {
def main(args: Array[String]): Unit = {
/*练习 4:成绩判断
定义:
val score = 85
按照下面规则输出:
90 ~ 100:优秀
80 ~ 89:良好
60 ~ 79:及格
0 ~ 59:不及格
例如:
score = 85
输出:
良好*/
val score = Random.nextInt(51) + 50
println(s"score = ${score}")
if (score >= 90 && score <= 100) {
println("优秀")
} else if (score >= 80 && score < 90) {
println("良好")
} else if (score >= 60 && score < 80) {
println("及格")
} else
println("不及格")
// GPT优化
if (score >= 90) {
println("优秀")
} else if (score >= 80) {
println("良好")
} else if (score >= 60) {
println("及格")
} else {
println("不及格")
}
}
}