Created
June 7, 2023 08:10
-
-
Save dkwktm45/7d6c0ba2a276030251d3e9b7d6921d8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.Scanner; | |
// 이진영 | |
class CustomComparator implements Comparator<String> { | |
@Override | |
public int compare(String o1, String o2) { | |
return o1.compareTo(o2); | |
} | |
} | |
public class ZeroReport07 { | |
static int j = 0; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
StringBuffer sb = new StringBuffer(); | |
System.out.println("[로또 당첨 프로그램]\n"); | |
int input = limit(sc); | |
String number; | |
String[] arr = new String[input * 7]; | |
for(char i = 65; i < 65 + input; i++){ | |
arr[j] = String.valueOf(i); | |
j++; | |
while(j % 7 != 0){ | |
number = null; | |
arr[j] = rdNum(number , arr, j); | |
j++; | |
} | |
Arrays.sort(arr, j - 6 < 0? 1 : j - 6 , j, new CustomComparator()); | |
} | |
int a = 1; | |
for(int i = 1; i <= arr.length / 7; i++){ | |
if(i == 1) { | |
sb.append(arr[0]+" "); | |
}else{ | |
sb.append(arr[a]+" "); | |
a++; | |
} | |
while(a < i*7){ | |
if(a + 1 == i*7){ | |
sb.append(arr[a] + "\n"); | |
}else{ | |
sb.append(arr[a] + ","); | |
} | |
a++; | |
} | |
} | |
sb.append("\n"); | |
String[] rotto = new String[6]; | |
for(int i = 0; i < rotto.length; i++){ | |
number = null; | |
rotto[i] = rdNum(number , rotto, i); | |
} | |
Arrays.sort(rotto); | |
sb.append("[로또 발표]\n"); | |
sb.append(" " + Arrays.toString(rotto).replace("[", "").replace("]", "").trim() + "\n\n"); | |
int cnt = 0; | |
for(int i = 0; i< arr.length; i++){ | |
if(i == 0){ | |
sb.append(arr[i] + " "); | |
}else if(i % 7 == 0){ | |
sb.append(arr[i] + " "); | |
}else{ | |
number = arr[i]; | |
for(int k = 0; k < rotto.length; k++){ | |
if(number.equals(rotto[k])){ | |
cnt++; | |
} | |
} | |
if((i+1) % 7 == 0){ | |
sb.append(number + " => " + cnt +"개 일치\n"); | |
cnt = 0; | |
}else{ | |
sb.append(number + ","); | |
} | |
} | |
} | |
System.out.println(sb); | |
} | |
private static boolean exists(String n[], String number , int j) { | |
for (int i = 0; i < j; i++) { | |
if(n[i].compareTo(number) == 0){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public static String rdNum(String number , String[] arr , int i){ | |
do { | |
number = String.valueOf((int) (Math.random()*45) + 1); | |
}while(exists(arr,number,i)); | |
return number.length() == 1 ? "0" + number : number; | |
} | |
public static int limit(Scanner sc){ | |
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):"); | |
int num = 0; | |
boolean checkB; | |
do{ | |
num = 0; | |
checkB = true; | |
try{ | |
num = Integer.valueOf(sc.next()); | |
}catch(NumberFormatException ex){ | |
System.out.print("숫자만을 입력해주시기 바랍니다."); | |
checkB = false; | |
}finally{ | |
if(checkB){ | |
if(num < 1 || 10 < num){ | |
System.out.print("로또 개수 제한은 1 - 10개 입니다. "); | |
checkB = false; | |
} | |
} | |
} | |
}while(!checkB); | |
return num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment