代码拉取完成,页面将自动刷新
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
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。