JAVA/문제풀이
[JAVA][프로그래머스] 로또 번호 생성
PeepPeep!
2022. 4. 9. 19:43
<문제>
로또 6개 1~45 사이 번호 생성
중복 x
public abstract class test {
public static void main(String[] args) {
int[] lotto = new int[6];
// 번호 생성
for(int i =0; i<6; i++) {
lotto[i] = (int)(Math.random()*45)+1;
// Math.random 0.0 ~1.0 미만의 난수
// 로또번호 1~45 이므로 *45 하여 0.0 ~ 45.0
// 후 +1 하면 1 이상 46 미만의 난수 생성
//중복값 제거
for(int j=0; j<i; j++) {
if(lotto[i] ==lotto[j]) {
i--;
break;
}
}
// 중복될 경우 i값을 -1 감소해줬으므로 다시 난수를 뽑을 때
// 다시 i 번째 값을 뽑으므로 중복됐던 값 위에 새로운 값이 덮어씌어진다
}
System.out.print("로또 번호 : ");
// 번호 풀력
for(int i =0; i<6; i++) {
System.out.print(lotto[i] + "/ ");
}
}
}