scala学习之路:2.Basic使用指南

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 }}

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值