### 演算
fun main(args: Array<String>){ // + - * / % val x = 10 println(x / 3) println(x / 3.0) println(x % 3.0) var y = 5 y++ println(y) y-- println(y) var z = 4 z += 12 // AND &&, or ||, Not ! val flag = true println(!flag) }
3
3.3333333333333335
1.0
6
5
false
### 文字列
fun main(args: Array<String>){ // 文字列 println("hello " + "world") val name = "yamada" println("my name is $name") println("my score is ${15 + 33}") // \n:改行 \t:タブ println("hello\n wor\tld") }
hello world
my name is yamada
my score is 48
hello
wor ld
### ifの条件分岐
val score = 85 if(score > 80){ println("great!") } else if(score > 60){ println("ok") } else { println("soso ..") }
> >= < <= == !=
三項演算子
[code]
val result = if(score> 80) “great” else “soso”
println(result)
[/code]
### when
// when val num = 3 when(num) { 0 -> println("Zero") 1 -> println("One") 2, 3 -> println("Two or Three") in 4..10 -> println("Many") else -> println("other") }
### while
var i = 0 while (i < 10){ println("loop: $i") i++ } do { println("loop2: $i") i++ } while(i < 5) [/code] ### for [code] for (i in 0..9){ if(i == 5) break println(i) } for (i in 0..9){ if(i == 5) continue println(i) } [/code] ### 関数 関数の引数は型を指定する [code] fun sayHi(name: String = "John", age: Int = 23){ println("hi! $name($age)") } fun main(args: Array<String>){ // for (変数) sayHi("tom", 22) sayHi() }
値を返す時は、関数側で型を指定する
fun sayHi(): String{ return "hi!" } fun main(args: Array<String>){ val msg = sayHi() println(msg) } fun sayHi() = "hi!"
-> 配列で返す場合はどうするんだろう??