알고리즘 문제연습/기초 알고리즘
[020] 369 삼육구 게임 코드
은z
2021. 6. 1. 22:30
문제
- 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(" ");
}
}
}