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
fun main(args: Array<String>) { | |
val text = "abcde" //String型の変数 | |
val number = text.toInt() //toInt()とはInt型に変換する。要するにtext変数の値をInt型にする。 | |
println("number = ${number}") //「abcde」はInt型に変換出来ないのでNumberFormatExceptionという例外が発生している。 | |
} |
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
fun main(args: Array<String>) { | |
printShow(5, 10) //2つの値の場合 | |
printShow(1, 2, 3) //3つの値の場合 | |
printShow(3, 3, 3, 3) //4つの値の場合 | |
} | |
fun printShow(vararg counts : Int){ //varargを使ってるので値の数に左右されず1つ用意すればOK | |
var result = 0 //ここで足し算用の変数を用意 | |
for(x in counts) result += x //for文で足していく | |
println(result) //結果を表示。値がいくつでも正しく表示されている。 |
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
fun main(args: Array<String>) { | |
printShow(5, 10) | |
printShow(1, 2) | |
} | |
fun printShow(a : Int, b : Int) : Unit{ //: Unitは書かなくても同じ意味になるので省略可能 | |
println(a * b) | |
} |
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
fun main(args: Array<String>) { | |
val ans = cal(3, 5, 8) //3つに増やした | |
println(ans) | |
} | |
fun cal(x : Int, y : Int, z : Int) = x * y * z //こっちも3つに増やした |
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
fun main(args: Array<String>) { | |
val ans = cal(3, 5) //ここで2つの値をかっこの中に書く。 | |
println(ans) | |
} | |
fun cal(x : Int, y : Int) = x * y //戻り値の型も省略出来る。 |
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
fun main(args: Array<String>) { | |
val ans = cal(3, 5) //ここで2つの値をかっこの中に書く。 | |
println(ans) | |
} | |
fun cal(x : Int, y : Int) : Int = x * y //この1行で1つの値を返してる |
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
fun main(args: Array<String>) { | |
val ans = cal(3, 5) //ここで2つの値をかっこの中に書く。 | |
println(ans) | |
} | |
fun cal(x : Int, y : Int) : Int{ //2つの値を変数xとyに代入 | |
println(x) //xが3であることが分かる。 | |
return x * y //main関数のかっこの中最初の数字がcal関数の最初の変数になってることが分かる | |
} |
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
fun main(args: Array<String>) { | |
val teki = kaisinnoichigeki(10) | |
println("${teki}のダメージ") | |
} | |
fun kaisinnoichigeki(a : Int) : Int = a + 5 //短縮したが先ほどと同じ内容です。 |
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
fun main(args: Array<String>) { | |
val teki = kaisinnoichigeki(10) //main関数から別の関数にInt型の値を送ってみました。 | |
println("${teki}のダメージ") //会心の一撃関数から受け取った値を表示しました。 | |
} | |
fun kaisinnoichigeki(a : Int) : Int{ //main関数から受け取った値をaの変数に代入してます。 | |
return a + 5 //aの変数に5を足して、その後returnでmain関数に戻してます。 | |
} |
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
fun main(args: Array<String>) { | |
val a : MutableMap<Int, String> = mutableMapOf(1 to "ねこ") | |
a.put(2 , "いぬ") //追加 | |
println("${a[1]} ${a[2]}") //そのまま表示 | |
val a3 = a.getOrDefault(1, "それは削除されました。") //「ねこ」が削除されていれば「それは削除されました」と表示されるようにした。 | |
println(a3) //ここでは削除されていないので「ねこ」と表示される | |
a.remove(1) //ここで「ねこ」を削除 | |