[原创]Scala学习:流程控制,异常处理
2016-05-22 17:52
411 查看
1.流程控制
1)do..while
2)for
方式一:
方式二:
for参数里面同样支持if判断。比如输出1——10中的偶数
增强for循环
3)while
4)if .. else .. (可以将结果直接赋值给一个变量)
注意:
1)Scala不支持i++和++i,需要使用i+=1来实现加一;
2.异常处理
try .. catch 机制,
1)do..while
def doWhile(){ var line="" do{ line = readLine() println("readlineConetent" + line ) }while(line != " ") }
2)for
方式一:
for (arg <-args) println(arg)
方式二:
for (i <- 1 to 10 if i % 2 == 0) println(i)
for参数里面同样支持if判断。比如输出1——10中的偶数
for (i <- 1 to 10 if i % 2 == 0) println(i)
增强for循环
//增强for循环 def doForStrenth(){ var files = (new java.io.File(".")).listFiles() for(file <- files){ println(file) } }
3)while
var i=0 while (i < args.length) { println (args(i)) i+=1 }
4)if .. else .. (可以将结果直接赋值给一个变量)
var file = if(args.isEmpty) args(0) else "spark" println(file)
注意:
1)Scala不支持i++和++i,需要使用i+=1来实现加一;
2.异常处理
try .. catch 机制,
//异常部分 def doException(){ val n = 99 val file = "scala.text" // OpenFileInputStreamAction try { val half = if (n % 2 == 0) n/2 else throw new RuntimeException("N must be event") } catch { case e: Exception => println("This exception is :" +e.getMessage() ) } finally { // close(file) }
相关文章推荐
- 学生科目成绩创建表与数据库
- [BS-24] UIImageView的contentMode属性
- hdu5698 百度之星2016round2b第3题
- AVR Studio 4.0 软件编程学习
- 第一天……
- codevs3249搭积木
- JavaScript挑战复杂报表
- swift 函数、函数类型及使用——swift学习(五)
- EPROTO: protocol error, symlink '../mime/cli.js'
- Fedora 普通用户和root用户切换
- 方格取数问题[网络流24题之9]
- 使用Toolbar/ActionBar自定义布局时,左侧/右侧有一段无法使用
- 怎样查看CentOS版本
- N^N的第一位数
- 【SHELL】监控系统信息CPU,磁盘,内存
- leetcode 4. Median of Two Sorted Arrays
- 第11课:Spark Streaming源码解读之Driver中的ReceiverTracker架构设计以及具体实现彻底研究
- Spring-IOC-BeanFactory
- Matlab绘图高级部分
- codevs 2964公共素数因数