码迷,mamicode.com
首页 > 其他好文 > 详细

SCALA常规练习A

时间:2015-04-26 12:21:47      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

package com.hengheng.scala

import scala.util.control.Breaks._
import scala.util.Random

object Application{
  def main(args : Array[String]) {
    println("Hello, World!")
    println(addSayHelloFirst(5, 10))
    val I = List(1, 2, 3)
    I.map((i) => {
      println("HI")
      println(i + 9)
    })
    
  }
  def addSayHelloFirst(i : Int, j : Int) : Int = {
    println("Hello")
    i + j
  }
  def add(i : Int, j : Int) = i + j
  val add2 = add(2, _ : Int)
  println(add2(99))
  def devide(i : Int)(j : Int) : Int = i / j
  val devideBy2 = devide(_ : Int)(2)
  println(devideBy2(100))
  var _100Devide = devide(100)(_ : Int)
  println(_100Devide(10))
  var k = 5
  if(k > 10) {
    println("k is more than 10.")
  }
  else if(k < 0) {
    println("k is less than 0")
  }
  else {
    println("k is between 1 and 10")
  }
  var i = 0
  var sum = 0
  while(i < 10) {
    sum += i
    i += 1
  }
  println(sum)
  sum = 0
  for (i <- 1 to 9) {
    sum += i
  }
  println(sum)
  
  breakable {
    while(true) {
      val r = new Random()
      val i = r.nextInt(10)
      println("i == " +i)
      if(i == 5) {
        break
      }
    }
  }
}

  输出:

101
50
10
k is between 1 and 10
45
45
i == 9
i == 0
i == 4
i == 9
i == 3
i == 1
i == 2
i == 1
i == 3
i == 5
Hello, World!
Hello
15
HI
10
HI
11
HI
12

SCALA常规练习A

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4457493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!