Scala第三天——Scala语法规范
自己的话:漆黑的黑夜 表示着威胁 我选择诙谐
工作在堆叠 没兑现归结于那些理解
Scala语句终结符、块表达式及输入输出
一、语句终结符
(1)默认情况下,scala不需要语句终结符,默认将每一行作为一个语句
(2)一行放多条语句:如果一行要放多条语句,则必须使用语句终结符
·例如,使用分号作为语句终结符:
var a, b, c = 0; if(a < 10) { b = b + 1; c = c + 1 }
通常来说,对于多行语句,还是会使用花括号的方式
if(a < 10) {
b = b + 1
c = c + 1
}
二、块表达式
块表达式,指的就是{}中的值,其中可以包含多条语句,最后一个语句的值就是块表达式的返回值。
例如,var d = if(a < 10) { b = b + 1; c + 1 }
三、输入和输出
1.print和println:
print打印时不会加换行符,而println打印时会加一个换行符。
·例如:
print("Hello World"); println("Hello World")
2.printf:
printf可以用于进行格式化
·例如:
printf("Hi, my name is %s, I'm %d years old.\n", "Leo", 30)
3.readLine:
readLine允许我们从控制台读取用户输入的数据,类似于java中的System.in和Scanner的作用。
4.综合案例:游戏厅门禁
val name = readLine("Welcome to Game House. Please tell me your name: ")
print("Thanks. Then please tell me your age: ")
val age = readInt()
if(age > 18) {
printf("Hi, %s, you are %d years old, so you are legel to come here!", name, age)
} else {
printf("Sorry, boy, %s, you are only %d years old. you are illegal to come here!", name, age)
}