문제 설명

솔루션
import java.util.*;
public class Solution {
public int solution(int n) {
int answer = 0;
String number = String.valueOf(n);
String[] arr = number.split("");
for(int i = 0; i < arr.length; i++){
answer += Integer.parseInt(arr[i]);
}
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
System.out.println(answer);
return answer;
}
}
다른 방법
import java.util.*;
public class Solution {
public int solution(int n) {
int answer = 0;
while(true){
answer+=n%10;
if(n<10)
break;
n=n/10;
}
// [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
System.out.println("Hello Java");
return answer;
}
}'java' 카테고리의 다른 글
| [프로그래머스 - 자바] 약수의 합 (0) | 2023.09.07 |
|---|---|
| [프로그래머스 - 자바] 짝수와 홀수 (0) | 2023.09.07 |
| [프로그래머스 - 자바] 옹알이(1) (0) | 2023.09.06 |
| [프로그래머스 -자바] 문자열 밀기 (0) | 2023.09.06 |
| [프로그래머스 -자바] 저주의 숫자 3 (0) | 2023.09.06 |