SMALL
1. 동작 원리
- 로또 번호 6개, 보너스 번호 1개를 랜덤하게 추출
- 사용자가 가지고 있는 로또번호 6개, 보너스 번호 1개를 입력받음
- 맞춘 개수: 6개 > 1등
5개 + 보너스 > 2등
5개(보너스 X) > 3등
4개 > 4등
3개 > 5등
0, 1, 2개 > 꽝
2. 코드
import java.io.*;
import java.util.*; // scanner, random
// 로또 프로그램 알고리즘
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int[] lotto = new int[6];
// 로또번호 저장 (1~45)
int[] user_lotto = new int[6];
// 사용자 입력 번호
int bonus, user_bonus;
// 보너스 번호, 사용자 입력 보너스 번호
int cnt = 0; // 동일 숫자 count 변수
// <로또>
for (int i = 0; i < 6; i++) {
lotto[i] = random.nextInt(45) + 1;
// 1~45
// * random.nextInt(x) => 0 ~ (x-1)
} // 로또 번호 6개
bonus = random.nextInt(45) + 1;
System.out.println("Lotto: " + Arrays.toString(lotto) + " bonus: " + bonus);
// <사용자>
System.out.print("숫자 6개 입력: ");
for (int i = 0; i < 6; i++) {
user_lotto[i] = sc.nextInt();
} // 로또 번호 6개
System.out.print("보너스 숫자 입력: ");
user_bonus = sc.nextInt();
System.out.println("User: " + Arrays.toString(user_lotto) + " bonus: " + user_bonus);
// 동일 숫자 개수 counting
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
if (lotto[i] == user_lotto[j]) {
cnt++;
}
}
}
System.out.println("총 맞춘 개수: " + cnt);
// 6개 > 1등 5개 + 보너스 > 2등
// 5개 > 3등 4개 > 4등 3개 > 5등
if (cnt == 6) {
System.out.println("1등");
} else if (cnt == 5) {
if (bonus == user_bonus) {
System.out.println("2등");
} else { // 보너스 점수가 다름
System.out.println("3등");
}
} else if (cnt == 4) {
System.out.println("4등");
} else if (cnt == 3) {
System.out.println("5등");
} else {
System.out.println("꽝");
}
}
}
3. 개념 짚기!
- Arrays.toString(배열) > 배열을 console에 보여줌! ex. [ 6 , 7, 19 ]
- Random random = new Random() > 난수 생성!
728x90
'JAVA' 카테고리의 다른 글
[멘토씨리즈] 22강 - 객체 배열 생성 (0) | 2023.05.26 |
---|---|
[멘토씨리즈] 16강 - 카페 주문 프로그램 제작 (0) | 2023.05.24 |
[JAVA] 모각코 활동 후기 :) (0) | 2021.08.30 |
[JAVA] 모각코 _ 15일차 (0) | 2021.08.27 |
[JAVA] 모각코 _ 14일차 (0) | 2021.08.26 |
댓글