Compare commits

..
2 Commits
6 changed files with 27 additions and 26 deletions
@@ -1,5 +1,6 @@
package practise1.comprehensive package practise1.comprehensive
object Comprehensive { object Comprehensive {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
/*第 1 课综合题 /*第 1 课综合题
@@ -19,16 +20,16 @@ object Comprehensive {
val score = 85 val score = 85
val evaluate = val evaluate =
if (score >= 90 ) "优秀" if (score >= 90) "优秀"
else if (score >= 80) "良好" else if (score >= 80) "良好"
else if (score >= 60) "及格" else if (score >= 60) "及格"
else "不及格" else "不及格"
println( println(
s""" s"""
|${name},今年${age}岁 |${name},今年${age}岁
|成绩:${score} |成绩:${score}
|评价:${evaluate}""".stripMargin) |评价:${evaluate}""".stripMargin)
} }
} }
@@ -1,5 +1,6 @@
package practise1.demo package practise1.demo
object Demo1VarAndVal { object Demo1VarAndVal {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
@@ -8,14 +9,13 @@ object Demo1VarAndVal {
// name = "Tom" // ❌ 不允许修改 // name = "Tom" // ❌ 不允许修改
var age = 25 var age = 25
age = 26 // ✅ 可以修改 age = 26 // ✅ 可以修改
println(name) println(name)
println(age) println(age)
println("=================================") println("=================================")
println("=================================") println("=================================")
} }
@@ -1,15 +1,16 @@
package practise1.demo package practise1.demo
object Demo2Type { object Demo2Type {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
//2. 类型 //2. 类型
// //
//Scala 支持自动推断类型: //Scala 支持自动推断类型:
val name = "xiaoMing" // String val name = "xiaoMing" // String
val age = 25 // Int val age = 25 // Int
val price = 19.99 // Double val price = 19.99 // Double
val flag = true // Boolean val flag = true // Boolean
// 也可以明确指定: // 也可以明确指定:
val name2: String = "xiaoMing" val name2: String = "xiaoMing"
+4 -5
View File
@@ -1,23 +1,22 @@
package practise1.demo package practise1.demo
object Demo4If { object Demo4If {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
// Scala 的 if 和 Java 类似 // Scala 的 if 和 Java 类似
val age = 20 val age = 20
if(age >=18){ if (age >= 18) {
println("成年人") println("成年人")
}else{ } else {
println("未成年") println("未成年")
} }
// 但是 Scala 有一个非常重要的特点:if 本身就是一个表达式,可以返回值 // 但是 Scala 有一个非常重要的特点:if 本身就是一个表达式,可以返回值
val result = if(age >= 18){ val result = if (age >= 18) {
"成年人" "成年人"
}else{ } else {
"未成年人" "未成年人"
} }
@@ -2,20 +2,21 @@ package practise1.exercises
object Exercises1 { object Exercises1 {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
/* 练习 1:基本变量 /* 练习 1:基本变量
创建三个变量: 创建三个变量:
name = "Tom" name = "Tom"
age = 20 age = 20
salary = 15000.5 salary = 15000.5
要求: 要求:
输出:Tom今年20岁,工资是15000.5*/ 输出:Tom今年20岁,工资是15000.5*/
val name = "Tom" val name = "Tom"
val age = 20 val age = 20
val salary = 15000.5 val salary = 15000.5
println(s"${name}今年${age}岁,工资是${salary}") println(s"${name}今年${age}岁,工资是${salary}")
} }
} }
@@ -1,5 +1,4 @@
package practise1.exercises package practise1.exercises
import scala.util.Random import scala.util.Random
object Exercises4 { object Exercises4 {