IT/알고리즘
88] 프로그래머스 숨어있는 숫자의 덧셈 (1) Kotlin
깻잎쌈
2023. 10. 23. 20:41
반응형

char는 toInt 했을때 int가 반환되지 않는다.
char.toString().toInt() 해줘야한다.
class Solution {
fun solution(my_string: String): Int {
var answer: Int = 0
for(i in 0 until my_string.length){
if(my_string[i] in '1' .. '9') {
answer += my_string[i].toString().toInt()
}
}
return answer
}
}
[Kotlin] Char을 Int로 변환하는 세가지 방법 알아보기 : Char to Int
Char을 Int로 변환할 때 많은 실수가 생기는 이유 Kotlin은 하나의 자료형을 다른 자료형을 변환할 때 to자료형 확장함수를 제공한다. 예를 들어 String을 Int로 변환할 때는 String.toInt()를 사용하면 된
kotlinworld.com

반응형