반응형
import java.util.Scanner;
public class Lmain {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[] myNum = new int[6];
int bonus;
Lotto lotto = new Lotto();
for(int i =0; i<myNum.length;i++) {
System.out.print(i+1+"번 번호를 입력해주세요 : ");
myNum[i] = sc.nextInt();
}
lotto.findMy(myNum);
}
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class Lotto {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
private HashSet<Integer> win = new HashSet<Integer>();
private Iterator<Integer> it = win.iterator();
private int bonus;
private int menu;
Lotto(){
do {
win.add(rand.nextInt(45)+1);
if(win.size() == 6)
break;
}while(true);
do {
bonus = rand.nextInt(45)+1;
}while(win.contains(bonus));
System.out.print("1등번호 : " + win);
System.out.println("\n2등 보너스 번호 : "+bonus);
}
void findMy(int[] my) {
boolean contain;
int cnt = 7;
for (int i = 0; i <my.length;i++) {
contain = win.contains(my[i]);
if(contain)
cnt--;
}
if(cnt == 2 && my[my.length-1] != bonus)
cnt ++;
if (cnt < 6)
System.out.println(cnt+ "등 당첨!");
else
System.out.println("꽝!");
System.out.print("몇등경우의 수를 보시겠습니까? :");
menu = sc.nextInt();
print(menu);
}
void print(int n) {
switch(n) {
case 1:{
System.out.println(win);
break;
}
case 2:{
for (int i = 0; i < win.size(); i++) {
int num = 0;
it = win.iterator();
while (it.hasNext()) {
if (num == i) {
System.out.print(bonus + " ");
it.next();
} else {
System.out.print(it.next() + " ");
}
num++;
}
System.out.println();
}
break;
}
case 3:{
for (int i = 0; i < win.size(); i++) {
int num = 0;
it = win.iterator();
while (it.hasNext()) {
if (num == i) {
System.out.print("X" + " ");
it.next();
} else {
System.out.print(it.next() + " ");
}
num++;
}
System.out.println();
}
break;
}
case 4:{
for(int i = 0; i < win.size(); i++) {
for (int j = i+1; j < win.size(); j++) {
int num1 = 0;
int num2 = 0;
it = win.iterator();
while (it.hasNext()) {
if (num1 == j) {
System.out.print("X" + " ");
it.next();
}
else if(num2 == i){
System.out.print("X" + " ");
it.next();
}
else {
System.out.print(it.next() + " ");
}
num1++;
num2++;
}
System.out.println();
}
}
break;
}
case 5:{
for(int i = 0; i < win.size(); i++) {
for(int j = i+1; j < win.size(); j++) {
for(int k = j+1;k < win.size();k++) {
int num1 = 0;
int num2 = 0;
int num3 = 0;
it = win.iterator();
while (it.hasNext()) {
if (num1 == k) {
System.out.print("X" + " ");
it.next();
}
else if(num2 == j){
System.out.print("X" + " ");
it.next();
}
else if(num3 == i){
System.out.print("X" + " ");
it.next();
}
else {
System.out.print(it.next() + " ");
}
num1++;
num2++;
num3++;
}
System.out.println();
}
}
}
break;
}
}
}
}
반응형
'코딩테스트' 카테고리의 다른 글
[JAVA] 이진탐색 재귀함수 구현 (0) | 2022.07.13 |
---|---|
[JAVA] 정수 받아 작은값부터 출력하기 (순열) (0) | 2022.07.11 |
[JAVA] baby-gin (0) | 2022.07.11 |
[JAVA] 정돈된 수 출력 (0) | 2022.07.11 |
[JAVA] 입력받은수 가장 작은 수로 변환 (0) | 2022.07.11 |