👨💻Programming/JAVA & C언어 예제
JAVA (자바) - 배열과 Math.random을 사용해 로또 번호 출력 프로그램
코너(Corner)
2020. 11. 9. 21:04
반응형
JAVA (자바) - 배열과 Math.random을 사용하여
로또 번호 출력 프로그램 만들기 , 단, 로또 번호는 중복이 나와서는 안된다.
(랜덤 으로 중복이 나올 경우를 대비)
프로그램을 Run 할 때마다 랜덤 번호가 생성 되며 보너스 번호까지 출력할 것
소스 코드
더보기
Scanner sc = new Scanner (System.in);
// 5. 로또 번호 출력하는 프로그램
// 규칙 1 ~ 45 숫자 랜덤
// 6개의 요소가 들어있는 1차원 배열에서 6개 값이 모두 1~45 안에 있는 숫자 랜덤넣게
int[] lotto = new int[7]; // 배열크기
System.out.println("인생 직진");
for(int i = 0; i < lotto.length; i++) {
lotto[i] = (int) ( Math.random() *45) + 1;
for (int j = 0; j < i; j++ ) {
if(lotto[i] == lotto[j]) {
i--;
break;
}
}
}
for (int i = 0; i < lotto.length-1; i++){
System.out.print(lotto[i]+" ");
}
System.out.println("마지막 보너스 : "+lotto[6]);
반응형