scala(5)Learn CH7 Programming in Scala from others
CH7 Build-in control structure
If statement
package com.sillycat.easyscala.lesson5
object TestIf {
def main(args: Array[String]): Unit = {
var filename = "default.txt"
println(args.isEmpty)
if (!args.isEmpty){
filename = args(0)
}
println("filename: " + filename)
//if statement will return the last sentence
val filename2 =
if (args.isEmpty) "default.txt" else args(0)
println ("filename2: " + filename2)
}
}
If statement has a return value.
For Statement
package com.sillycat.easyscala.lesson5
object TestFor {
def main(args: Array[String]): Unit = {
val files = (new java.io.File(".")).listFiles()
println("==========file names1 :")
for (file <- files) {
print(file + " ")
}
println();
println("==================================");
println("==========file names2 :")
for (f <- files) print(f + " ")
println();
println("====================================");
// 1 to 5 will return Range(1, 6)
// 1, 2, 3, 4, 5
println("==========numbers 1 :")
for (i <- 1 to 5) print(i + " ")
println();
println("=======================================");
// 0 until 5 will return Range(0, 5)
// 0, 1, 2, 3, 4
println("===========numbers 2: ");
for (i <- 0 until 5) print(i + " ")
println();
println("============================");
// multiple checkpoint
for (
file <- files if (file.getName.endsWith("bin"))
) println(file)
// for
for (
file <- files
if (file.isFile)
if (file.getName.endsWith("gitignore"))
) println(file)
//put all the matched files in array
val sfiles = for (
file <- files if (file.isFile)
) yield file
// files in array
for (i <- 0 to sfiles.length - 1){
print(sfiles(i))
}
}
}
Try Catch System almost the same to Java
package com.sillycat.easyscala.lesson5
import java.io.FileReader
object TestTry {
def main(args: Array[String]): Unit = {
var f = new FileReader("test.txt")
try {
// use the file
} catch {
case ex: java.io.FileNotFoundException =>
println(ex)
case ex: java.io.IOException =>
println(ex)
} finally {
f.close()
}
}
}
Match is much better than switch, not matter int, even string is working.
package com.sillycat.easyscala.lesson5
object TestMatch {
def main(args: Array[String]): Unit = {
val firstArg = if (!args.isEmpty) args(0) else ""
var n = 10000
var friend = "adsfadfasf"
firstArg match {
case "salt" =>
friend = "pepper"
n = 100
case "chips" =>
friend = "salsa"
n = 10
case "eggs" =>
friend = "bacon"
n = 1
//this is the default value
case _ =>
friend = "huh?"
n = 0
}
println(friend + n.toString());
}
}
references:
http://sillycat.iteye.com/blog/1536392
http://snowriver.org/blog/tag/scala/
http://snowriver.org/blog/2011/03/20/programming-in-scala-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0ch7-built-in-control-structure/
CH7 Build-in control structure
If statement
package com.sillycat.easyscala.lesson5
object TestIf {
def main(args: Array[String]): Unit = {
var filename = "default.txt"
println(args.isEmpty)
if (!args.isEmpty){
filename = args(0)
}
println("filename: " + filename)
//if statement will return the last sentence
val filename2 =
if (args.isEmpty) "default.txt" else args(0)
println ("filename2: " + filename2)
}
}
If statement has a return value.
For Statement
package com.sillycat.easyscala.lesson5
object TestFor {
def main(args: Array[String]): Unit = {
val files = (new java.io.File(".")).listFiles()
println("==========file names1 :")
for (file <- files) {
print(file + " ")
}
println();
println("==================================");
println("==========file names2 :")
for (f <- files) print(f + " ")
println();
println("====================================");
// 1 to 5 will return Range(1, 6)
// 1, 2, 3, 4, 5
println("==========numbers 1 :")
for (i <- 1 to 5) print(i + " ")
println();
println("=======================================");
// 0 until 5 will return Range(0, 5)
// 0, 1, 2, 3, 4
println("===========numbers 2: ");
for (i <- 0 until 5) print(i + " ")
println();
println("============================");
// multiple checkpoint
for (
file <- files if (file.getName.endsWith("bin"))
) println(file)
// for
for (
file <- files
if (file.isFile)
if (file.getName.endsWith("gitignore"))
) println(file)
//put all the matched files in array
val sfiles = for (
file <- files if (file.isFile)
) yield file
// files in array
for (i <- 0 to sfiles.length - 1){
print(sfiles(i))
}
}
}
Try Catch System almost the same to Java
package com.sillycat.easyscala.lesson5
import java.io.FileReader
object TestTry {
def main(args: Array[String]): Unit = {
var f = new FileReader("test.txt")
try {
// use the file
} catch {
case ex: java.io.FileNotFoundException =>
println(ex)
case ex: java.io.IOException =>
println(ex)
} finally {
f.close()
}
}
}
Match is much better than switch, not matter int, even string is working.
package com.sillycat.easyscala.lesson5
object TestMatch {
def main(args: Array[String]): Unit = {
val firstArg = if (!args.isEmpty) args(0) else ""
var n = 10000
var friend = "adsfadfasf"
firstArg match {
case "salt" =>
friend = "pepper"
n = 100
case "chips" =>
friend = "salsa"
n = 10
case "eggs" =>
friend = "bacon"
n = 1
//this is the default value
case _ =>
friend = "huh?"
n = 0
}
println(friend + n.toString());
}
}
references:
http://sillycat.iteye.com/blog/1536392
http://snowriver.org/blog/tag/scala/
http://snowriver.org/blog/2011/03/20/programming-in-scala-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0ch7-built-in-control-structure/
本文深入探讨了Scala编程语言中的内置控制结构、条件语句、循环语句等核心概念,并通过实例展示了如何在Scala中使用if语句、for循环、try-catch块以及匹配表达式。同时,还介绍了Scala中的一些高级特性,如模式匹配和类型推断,旨在帮助开发者更高效地进行Scala编程。
103

被折叠的 条评论
为什么被折叠?



