-
LV2 메모장 프로그램 제출날인데 어제 8시에 진행했던 실시간 강의가 늦게 지급되어
학습에 차질이 생겨 강의 제출하지 못했습니다.실시간 강의가 지급되기전에 알고리즘 문제 하나 풀었고 심화 강의 복습하다
저번주에 진행했던 후발대 교육에서 파생된 Random을 이용한 로또 코딩을 완성했습니다.List<Integer> lottoList = new ArrayList<>(6);
public void lottoList() { lottoList.clear(); // 다른 변수로 반복 실행할거라 실행마다 초기화 시켜줌 lottoList.add(num = rd.nextInt(45) + 1); // 첫값은 비교할 필요가 없어서 바로 넣어줌 for (x = 0; x < 10; x++) { num = rd.nextInt(45) + 1; if (lottoList.size() == 6) { break; } for (int rusult : lottoList) { if (rusult != num) { lottoList.add(num); break; } else { break; } } } System.out.println("로또 예상번호 : " + lottoList); }
위 ArrayList로 만드려고 고집부리다가 두번째 for문 같은 수 필터링을 해줬는데
적용이 안되서 이를 해결하기 위해 3시간 이상 박고 정신이 몽롱해져서
튜터님 추천으로 HashSet으로 해결했습니다.Set<Integer> lottoList = new HashSet<>(6);
public void Repetition() { // 반복 실행 System.out.println("반복 횟수를 입력하세요"); int scNumber = sc.nextInt(); for (i = 0; i < scNumber; i++) { lottoList(); } } public void lottoList() { lottoList.clear(); for (x = 0; x < 10; x++) { lottoList.add(num = rd.nextInt(45) + 1); if (lottoList.size() == 6) { // Set이 중복을 걸러줘서 size = 6 탈출 break; } } System.out.println("로또 예상번호 : " + lottoList); }
담부턴 아는걸로만 사용하겠다 깝치지말고 자료를 찾아보는식으로 하겠다 다짐하게된 하루..