Last active
September 24, 2015 16:51
-
-
Save Sankame/7f45c2231a524ad6e7a8 to your computer and use it in GitHub Desktop.
メソッドと関数オブジェクト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util._ | |
object myapp { | |
//メソッド | |
def total(num: Int) = { | |
var res:Int = 0 | |
for(n:Int <- Range(1,num + 1)) | |
res += n | |
res | |
} | |
def main(args: Array[String]) = { | |
//関数オブジェクト | |
val total = (num: Int) => { | |
var res:Int = 0 | |
for(n:Int <- Range(1,num + 1)) | |
res += n | |
res | |
} | |
val rnd = new Random | |
val n = rnd.nextInt(100) | |
println(n + "までの合計:" + total(n)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment