JAVA

자바예제 02 로또 프로그램

남기루 2019. 6. 19. 09:13
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package homeW;
 
public class Lotto {
 public static void main(String[] args) {
 
  int[] lottoNum= new int[6]; //배열 초기화
 
  for(int i =0;i<=5;i++) { //6번 반복
   int count=0//중복된 값을 확인하기 위함
   int landomNum = (int)(Math.random()*45+1); //1부터 45까지의 수중 랜덤 숫자 초기화
   for(int j=0;j<=5;j++) {
    if(lottoNum[j]==landomNum) { //인덱스 0~5 까지 수중 난수와 같은 값이 있을경우 count++
     count++;
    }
   }
   if(count == 0) { //중복된 수가 없을경우 난수 저장
    lottoNum[i] = landomNum;
   }
   else { //중복된 수가 있을경우 i--를 통해 다시 실행
    i--;
   }
  }
  for(int i=0;i<=5;i++//출력
   System.out.print(" "+lottoNum[i]);
 }
}
 
 

실행결과(정렬기능X)

 

반응형