2,Scala函数定义、流程控制、异常处理入门实战

package com.dt.scala.hello

object ScalaBasics {

  //  var line = ""
  //  do {
  //    println("Please input some words blow......")
  //    line = readLine()
  //    println("Read: " + line)
  //  } while (line != "")

  def main(args: Array[String]) {
    println("This is Spark!!!")
    for (arg <- args)
      println(arg)

    //    var file = "scala.txt"
    //    if (!args.isEmpty) file = args(0)
    //    val file = if (!args.isEmpty) args(0) else "scala.xml"
    //    println(file)

    println(if (!args.isEmpty) args(0) else "Spark.xml")

    println(looper(100, 298))
    //    doWhile

    for (i <- 1 to 10) {
      println("Number is :" + i)
    }

    val files = (new java.io.File(".")).listFiles()
    for (file <- files) {
      println(file)
    }

    val n = 99
    val file = "Spark.txt"
    // openFile(file)
    try {
      val half = if (n % 2 == 0)
        n / 2
      else throw
        new RuntimeException("N must be event")
      // Use the file
    } catch {
      case e: Exception => println("The exception is :" + e.getMessage())
    } finally {
      // close(file)
    }
  }

  def looper(x: Long, y: Long): Long = {
    var a = x
    var b = y
    while (a != 0) {
      val temp = a
      a = b % a
      b = temp
    }
    b
  }

  def doWhile() {
    var line = ""
    do {
      line = readLine()
      println("Read: " + line)
    } while (line != "")
  }
}


信息来源于 DT大数据梦工厂微信公众账号:DT_Spark

网址:http://pan.baidu.com/share/home?uk=4013289088&view=share#category/type=0


你可能感兴趣的:(2,Scala函数定义、流程控制、异常处理入门实战)