-> 같은 숫자가 있으면 안되기 때문에 앞에 숫자와 먼저 비교하고 같지 않다면 저장하기!
-> 숫자가 같다면 다시 진행해야 되기 때문에 for문안에 if문 넣기
int[] arr_ball = new int[45]; // 45개의 공
int[] arr_temp = new int[5]; // 기존에 뽑은 공 기억하기
for(int i=0; i<arr_ball.length; i++) { // 공뽑기
arr_ball[i] = i+1; // 공 번호 1~45 까지 설정
} // end of for------------------
==> 만약 공번호 101번 부터 하고 싶을경우 +101로 변경
Random rnd = new Random();
String result ="";
outer:
for(int i=0; i<6; i++) { // 6번 뽑기
int idx = rnd.nextInt(44-0+1)+0; // 여기서 i 는 방번호이므로 시작값은 0
// idx = 뽑은 공 기억
for(int j=0; j<arr_temp.length; j++) { // j < 5 -> 6번 뽑기 진행
==> 앞에 숫자와 비교 먼저 진행 !!
if(idx == arr_temp[j]){ // 새로 뽑은 공이 기존 뽑은 공과 같다면
i--; // 재진행하기위해 i--를 해준다.
continue outer; // outer라는 레이블명의 반복문으로 이동하여 다시 진행
// ==> break 를 쓰면 아래 저장하기로 넘어가기때문에 사용 X
} // end of if-----------
} // end of for---------------------------------------
==> 앞에 숫자와 같지 않다면, 저장하기!!
if(i<5) {
arr_temp[i] = idx;
} // end of if---------
String add = (i<5)?",":"";
result += idx + add;
} // end of for----------------
System.out.println("\n로또 1등 당첨번호 : " + result);
-> 랜덤 값을 배열에 먼저 저장한 후 비교하기
-> ' i-- ' 를 통해 값을 덮어쓰기 해준다.
public static void main(String[] args) {
Random random = new Random();
int[] number = new int[6]; //로또 번호 출력
for(int i=0; i<number.length; i++) {
number[i] = random.nextInt(6) + 1; // 랜덤 값을 배열에 넣는다
for (int j = 0; j<i; j++) { //j는 i까지 돈다(입력한 값들만 체크할거라서)
if(number[i] == number[j]) { // 인덱스 안 값이 서로 같으면
i--;
break; //밖으로 나오면 증감식 해주기때문에 i--한뒤 break
} // end of if------------
} // end of for---------------------
} // end of for------------------
for (int i=0; i<number.length; i++) {
String add = (i == 0)? "":",";
System.out.print(add + number[i]);
} // end of for------------
} // end of main()----------------------------------
Random : https://velog.io/@jjoung-2j/Random
방법2 : https://velog.io/@nime0110/java-%EB%B0%98%EB%B3%B5%EB%AC%B8-%EB%B0%B0%EC%97%B4-%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C-%EB%A1%9C%EB%98%90-%ED%9A%8C%EC%9B%90%EA%B0%80%EC%9E%85%ED%9A%8C%EC%9B%90%EB%AA%A9%EB%A1%9D-%EC%B6%9C%EB%A0%A5-%ED%95%99%EC%A0%90%ED%8F%89%EA%B7%A0%EB%93%B1%EC%88%98-%EC%B6%9C%EB%A0%A5%ED%95%98%EA%B8%B0
my.day08.b.array -> Main_lotto