1 Star 0 Fork 0

kergp/scalaImpatient

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ControlExpression.scala 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
kergp 提交于 6年前 . modify
package com.garuda.practice2
import scala.util.control.Breaks._
/**
* @author : stopherguo
* @Description: 练习scala控制表达方式,包括条件、循环、
* @version : 0.1
* @Date create time : 2020-01-17
* @Modify by
*
**/
object ControlExpression {
/**
* 条件
* @param condition
* @return
*/
def conditional(condition: Int): Unit ={
if(condition > 5){
println("输入数据大于5")
}else{
println("输入数据小于5")
}
}
/**
* break 使用 break 跳出循环
* @param i
* @return Int
*/
def testBreak(i: Int): Int ={
for(a <- 1 to 10){
if(a == i){
break()
}
println("break number: " + a)
a
}
1+1;
// throw new Exception("out bound of array")
}
/**
* while 循环
*/
def testWhile()={
var n = 10
while (n > 0){
println("while's loop time :" + n)
n -= 1
}
}
/**
* for 循环
*/
def testFor()={
var n = 10
for(i <- 1 to n){
println("for's loop time: " + i)
}
}
/**
* 多个循环使用;分割
*/
def advanceLoop()={
for ( i : Int <- 1 to 10 ; j: Int <- 1 to 10 ) {
print(10 * i + j + " ")
if(j == 10){
println(" ")
}
}
}
/**
* 多个循环使用;分割,还可以用if添加守卫表达式
*/
def advanceLoopAddIf()={
for ( i : Int <- 1 to 10 ; j: Int <- 1 to 10 if i == j) {
println(10 * i + j + " ")
// if(j == 10){
// println(" ")
// }
}
}
def testYield(): Unit ={
val str = for (c <- "hello"; i <- 0 to 1) yield{
(c + i ).toChar
}
println(str.getClass)
val set = for ( i <- 0 to 1; c <- "hello" ) yield {
(c + i ).toChar
}
println(set.toString())
}
//测试主程序
def main(args: Array[String]): Unit = {
// conditional(8)
//// testBreak(6)
//
// /** scala不支持三目运算 **/
// val i = 1
//// (i > 0) ? 1:2
// /** if语句代替三目运算 **/
// if(i>0) 1 else 2
//
// testWhile
// testFor()
//高级循环
// advanceLoop
advanceLoopAddIf
// 测试yield
testYield
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Scala
1
https://gitee.com/kergp/scalaImpatient.git
git@gitee.com:kergp/scalaImpatient.git
kergp
scalaImpatient
scalaImpatient
master

搜索帮助