728x90
언어_자바
프로그래머스_수 조작하기1
public class Control1 {
public static void main(String[] args) {
Test4 t = new Test4();
int n = 0;
String control = "wsdawsdassw";
t.test4(n, control);
}
}
class Test4{
public int test4(int n, String control) {
for (char cmd : control.toCharArray()) { // 문자열을 char 배열로 변환
switch (cmd) {
case 'w':
n += 1;
break;
case 's':
n -= 1;
break;
case 'd':
n += 10;
break;
case 'a':
n -= 10;
break;
}
}
System.out.println(n); // 입력에 대한 마지막 n 값
return n;
}
}
Key Point 2
1. toCharArray() 를 이용하여 문자열을 char 배열로 변환
2. switch~case~break 문을 이용하여 각각의 조건에 맞는 값 반환
728x90
'Algorithm' 카테고리의 다른 글
[이것이 코딩 테스트다] (Ch03 그리디) 큰 수의 법칙 (0) | 2023.12.25 |
---|---|
[프로그래머스] (181925) 수 조작하기2 (0) | 2023.12.24 |
[프로그래머스] (181927) 마지막 두 원소 (0) | 2023.12.24 |
[프로그래머스] (181928) 이어 붙인 수 (1) | 2023.12.24 |
[프로그래머스] (181932) 코드 처리하기 (0) | 2023.12.23 |