소스 코드
package superCode;
import java.util.Random;
// [문제]
// 1. 1~50사이의 랜덤 숫자를 저장한다.
// 2. 랜덤 숫자 중에서 3이나 6이나 9의 개수가 2개면, 짝짝을 출력한다.
// 3. 랜덤 숫자 중에서 3이나 6이나 9의 개수가 1개면, 짝을 출력한다.
// 4. 랜덤 숫자 중에서 3이나 6이나 9의 개수가 0이면, 해당 숫자를 출력한다.
// 예)
// 33 => 짝짝
// 16 => 짝
// 7 => 7
public class Randum369 {
public static void main(String[] args) {
// 1. 1~50사이의 랜덤 숫자를 저장한다.
Random ran = new Random();
int rNum = ran.nextInt(50); // [0 ~ 49] + 1
System.out.println("rNum = " + rNum);
// 십의 자리
int tens = rNum / 10;
// 일의 자리
int units = rNum % 10;
System.out.println("십의 자리 = " + tens);
System.out.println("일의 자리 = " + units);
int count = 0;
// 십의 자리 3, 6, 9 개수
if(tens == 3 || tens == 6 || tens == 9) {
count = count + 1;
}
// 일의 자리 3, 6, 9 개수
if(units == 3 || units == 6 || units == 9) {
count = count + 1;
}
if(count == 2) {
System.out.println("짝짝");
}
else if(count == 1) {
System.out.println("짝");
}else {
System.out.println(rNum);
}
}
}
결과
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정 > 문제 풀어보기' 카테고리의 다른 글
연산자 기호 맞추기 게임 3 ? 4 = 12 (0) | 2023.06.05 |
---|---|
Randum_가운데 숫자 입력 후 정답, 오답 확인 ( 150~250 3자리 값 ) (0) | 2023.06.05 |
이름_전화번호 입력_검색(반복문 활용) (0) | 2023.06.03 |
if문 중괄호 생략 (0) | 2023.05.30 |
Random_임의의 숫자 (0) | 2023.05.30 |