package com.scala01.hello
object ScalaBasics {
def main(args: Array[String]): Unit = {
val file = if (!args.isEmpty) args(0) else "scala.xml" // 如果运行之前没有传递参数的话就会打印出:<span style="font-family: Arial, Helvetica, sans-serif;">scala.xml</span>
// 此处的if else 就像是三目预算 int b =0; int a = b == 0 ?!:2;
println(file)
doWhile // 或者是 doWhile() 右边有括号更适用于有参数的调用
println(looper(100, 298))
// for 循环
// for(i <- 1 to 10) println(i) 简写
// for (i <- 1.to(10)) println(i) // 一切皆对象
// 增强for
// val files = (new java.io.File(".")).listFiles()
// for (file <- files) println(file)
val n = 99
val file2 = "spark.txt"
// openFile(file2)
try {
val half = if (n%2 ==0) n/2 else throw
new RuntimeException("N must be event")
} catch {
case e :Exception => println("The exception is :"+e.getMessage)
// 异常的捕获
}finally {
// close(file2)
}
}
def doWhile() {
var line = "" // 此处的 "" 表示为空或者用 _ 下划线来占位
do {
line = readLine() // 貌似现在是已经废弃了
// line = StdIn.readLine()
println("Read :" + line)
} while (line != "")
println("Input end!")
}
// // 静态代码块不调用也会执行 <pre name="code" class="objc"> //这是一个简单的doWhile循环使用
// var line = "" // do { // println("please input some words for testing") // line = readLine() // 貌似是已经废弃了 这句的使用和java中很像 // println("Read :" + line) // } while (line != "") // // println("Input end!") def looper(x: Long, y: Long): Long = { // 注意此处的最后面的Long ,代表返回的值得类型
// Unit 表示为空 打印出来既是一个()
//这是一个简单的while循环使用 var a = x var b = y while (a != 0) { var temp = a a = b % a b = temp } b }}