문제
- 3,6,9 게임을 코드로 구현하세요
보완할 점
소스코드
public class Algorithm20 {
public static void main(String[] args) {
//3 6 9 게임, 100까지
String[] str = new String[100];
int snum = 1;
//1~100까지 배열에 담아두기
for(int i = 0; i < str.length; i++) {
str[i] = snum + "";
snum++;
}
//하나씩 꺼내서 검사
for(int i = 0; i < str.length; i++) {
boolean flag = true; //flag 초기화
for(int j = 0; j < str[i].length(); j++) {
if( (str[i].charAt(j)-'0') % 3 == 0 && str[i].charAt(j)-'0' != 0) {
System.out.print("짝");
flag = false;
}
}
if(flag) { //위의 if문에 한번도 안들어갔으면 여기로 들어와라
System.out.print(str[i]);
}
System.out.print(" ");
}
}
}
'알고리즘 문제연습 > 기초 알고리즘' 카테고리의 다른 글
[019] 거듭제곱 코드 (0) | 2021.06.02 |
---|---|
[018] 별 찍기 (하나씩 증가) (0) | 2021.06.02 |
[017] 별 찍기 (하나씩 감소) (0) | 2021.05.29 |
[016] 별 찍기 (하나씩 증가) (0) | 2021.05.29 |
[015] 구구단 출력하기 (0) | 2021.05.29 |
댓글