MSM8994의 화룡점정 블로그

[JAVA] 369 게임 본문

코딩

[JAVA] 369 게임

msm8994 2018. 7. 18. 07:00

여기서는 해당 수를 문자열로 변환해 3 6 9 인 문자 갯수대로 "짝"을 출력하거나 없는 경우 수를 출력하여 다음으로 넘어갑니다.

cnt <= 400 에서 400을 10000 처럼 다른 수로 바꾸면 9999까지 갑니다.


for (int cnt = 1; cnt <= 400; cnt++) {
	String strCnt = String.valueOf(cnt); // 문자열 변환
	boolean isChark = false; // 3 6 9 해당여부 저장
	for (int i = 0; i < strCnt.length(); i++) {
		char chk = strCnt.charAt(i);
		// 해당 수의 앞에서부터 i번째 숫자부터 검사
		if (chk == '3' || chk == '6' || chk == '9') {
			// 3 6 9 해당되면 짝 출력
			System.out.print('짝');
			isChark = true;
		}
	}
	if (!isChark) {
		// 3 6 9 해당 되지 않았다면 수 표시
		System.out.print(strCnt);
	}
	if (cnt % 10 == 0) {
		// 10 단위로 줄바꿈
		System.out.println();
	} else {
		// 중간수면 한 칸 띄우기
		System.out.print(' ');
	}
}


결과


Comments