import scala.util.control._
import scala.util.control.Breaks._
/**
* @Author jdj
* @Date 2021/3/29 4:30 下午
* @Version 1.0
*
* 语法:
* 创建break 对象
* val loop = new Breaks
* 在 breakable 里面循环
* loop.breakable{
* for(i <- 1 to 10){
* loop.break
* }
* }
*/
object BreakDemo {
def main(args: Array[String]): Unit = {
val loop = new Breaks
loop.breakable {
for (i <- 1 until 10) {
println("i :" + i)
if (i == 4) {
loop.break()
}
}
}
breakable {
for (i <- 1 to 10) {
println(" ii : " + i)
if (i == 5) {
break()
}
}
}
var loop2 = true
for (i <- 1 to 10 if loop2 == true) {
println(" iii : " + i)
if (i == 5) {
loop2 = false
}
}
}
}
网友评论