๊ด€๋ฆฌ ๋ฉ”๋‰ด

Vintage appMaker์˜ Tech Blog

[kotlin] associate๋ฅผ ์ด์šฉํ•œ list → Map ๋ณธ๋ฌธ

Source code or Tip/Android(Java, Kotlin)

[kotlin] associate๋ฅผ ์ด์šฉํ•œ list → Map

VintageappMaker 2020. 12. 29. 11:32

๐Ÿ“ข ๋ฆฌ์ŠคํŠธํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ map ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. 

fun main(args: Array<String>) {

    // ์ˆœ์ฐจ์ ์ธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ map ์ž‘์„ฑ
    val player_reward = (1..100).associate{
        it to reward(it)
    }

    (1..50).forEach { println ("${it}๋“ฑ -> ${player_reward[it]}")  }
}

fun reward(n: Int, baseMoney : Int = 1000): Int =
        // if else์˜ ๊ฐ„์†Œํ™” ๋ชฉ์ 
        when {
            n == 1     -> baseMoney * 10
            n in 2..3  -> baseMoney * 5
            n in 4..10 -> baseMoney * 2
            else       -> baseMoney
        }
 

associate - Kotlin Programming Language

 

kotlinlang.org

 

 

 

 

 

Comments