Skip to content

Instantly share code, notes, and snippets.

@rorrorome
Last active July 7, 2023 17:08
Show Gist options
  • Select an option

  • Save rorrorome/281d404584bb5e1488a0ea6abb4df450 to your computer and use it in GitHub Desktop.

Select an option

Save rorrorome/281d404584bb5e1488a0ea6abb4df450 to your computer and use it in GitHub Desktop.
로또
/*
* 제로베이스 백엔드 스쿨 15기
* 한새롬
* 미니과제 7. 로또당첨 프로그램
*
* Scanner의 입력함수와 조건문 및 반복문과 배열을 통한 로또 당첨 로직 작성
* 1. 로또 구매 수량 입력
* 2. 입력한 개수만큼의 로또 개수 생성
* 3. 로또 당첨 번호 생성(숫자값은 중복 배제 및 정렬해서 표시)
* 4. 당첨 번호와 구매 로또 비교하여 숫자 일치 여부 판단
* 5. Collections.shuffle 함수 사용 금지!(shuffle함수는 과제의 취지와 맞지 않기 때문에, 사용시 0점 처리)
*/
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class MiniProject07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("[로또 당첨 프로그램]");
System.out.println();
System.out.print("로또 개수를 입력해 주세요.(숫자 1~10):");
int amount = sc.nextInt();
String[] game = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
int[][] my = new int[amount][6];
for (int i = 0; i < amount; i++) {
int[] myn = new int[6];
Random mine = new Random();
for (int x = 0; x < 6; x++) {
myn[x] = mine.nextInt(45) + 1;
for (int y = 0; y < x; y++) {
if (myn[x] == myn[y]) {
x--;
}
}
}
Arrays.sort(myn);
my[i] = myn;
System.out.printf("%s\t%02d, %02d, %02d, %02d, %02d, %02d\n", game[i], myn[0], myn[1], myn[2], myn[3], myn[4], myn[5]);
}
System.out.println();
System.out.println("[로또 발표]");
int[] lucky = new int[6];
Random lk = new Random();
for (int i = 0; i < 6; i++) {
lucky[i] = lk.nextInt(45) + 1;
for (int j = 0; j < i; j++) {
if (lucky[i] == lucky[j]) {
i--;
}
}
}
Arrays.sort(lucky);
System.out.printf("\t%02d, %02d, %02d, %02d, %02d, %02d\n", lucky[0], lucky[1], lucky[2], lucky[3], lucky[4], lucky[5]);
System.out.println();
System.out.println("[내 로또 결과]");
for (int i = 0; i < amount; i++) {
int[] n = new int[amount];
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++) {
if (my[i][k] != lucky[j]) {
continue;
} else {
n[i]++;
}
}
}
System.out.printf("%s\t%02d, %02d, %02d, %02d, %02d, %02d => %d개 일치\n", game[i], my[i][0], my[i][1], my[i][2], my[i][3], my[i][4], my[i][5], n[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment