Array , List 연습 코드
2022. 4. 7. 16:55ㆍIT/안드로이드
반응형
하 ㅡㅡ
낮설다 코틀린 너란녀석... ㅜ_ㅜ~
fun main(args: Array<String>) {
//println("\nYour fortune is : ${getFortuneCookie()}")
var fortune: String
for ( i in 1..10 ) {
fortune = getFortuneCookie()
println("\nYour fortune is : $fortune")
if(fortune.contains("Take it easy")) break
}
}
fun getFortuneCookie(): String{
val Fortunes = listOf("You will have a great day!", //0
"Things will go well for you today", //1
"Enjoy a wonderful day of success", //2
"Be humble and all will turn out well", //3
"Today is a good day for exercising restraint", //4
"Take it easy and enjoy life!", //5
"Treasure your friends, because they are your greatest fortune." //6
)
print("\nEnter your birthday: ")
val birthday = readLine()?.toIntOrNull() ?: 2
return Fortunes[birthday.rem(Fortunes.size)]
}
getFortuneCookie() 는 String 값 return 해주고...
Fortune 에 list넣어서 ... birthday값 받아서 값에 해당하는 string 값 뿌려준다. 여기서 bday에 null 값 들어갈수도 있으니
toIntOrNull 로 null 값 확인..
10회 시도시 Take it easy 뿌리고 break..
얼마나 더 해야 익숙해질까..
C 고인물은 웁니다..
728x90
반응형
'IT > 안드로이드' 카테고리의 다른 글
Android - Color 값 불러오기. (7) | 2022.04.11 |
---|---|
Android Kotlin - Unresolved reference Error (4) | 2022.04.11 |
Kotlin Null 처리 (Not null, Nullable) (4) | 2022.04.07 |
Android - Jetpack, MVVM, MVC (5) | 2022.04.06 |
Android - View/Data binding (+ 문제점들) (3) | 2022.04.06 |