Back-end/JAVA,Spring

자바 로또 메이커 소스코드

cheersHena 2018. 6. 2. 22:32
반응형
로또 메이커 만들기!!! 




int [] lotto= lm.make(); // lotto값 [6, 13, 25, 29, 31, 45]
lm.printPaper(lotto); 실행 시 

출력 값:
01 02 03 04 05 **  07
08 09 10 11 12 **  14
15 16 17 18 19 20 21
22 23 24 **  26 27 28
**  30 **  32 33 34 35
36 37 38 39 40 41 42 
43 44  **


완성 소스코드.
package chapter_06;

public class LottoMaker2 {
    int[] deck = new int[45];
    int[] lotto = new int[6];

    void initialization() { // 1-45까지 숫자만드는 메서드
        for (int i=0; i<deck.length; i++) {
            deck[i] = i+1; //1-45까지 값 초기화.
        }
    }

    void mix() { //섞기 메서드
        for (int i=0; i<100; i++) { 
            int n = (int) (Math.random() * 45);//랜덤숫자만들기

            //2) 랜덤째공간숫자와 맞바꾸기.막섞기.
            int tmp = deck[0];
            deck[0] = deck[n];
            deck[n] = tmp;
        }
    }

    void extract() {

        for(int j=0; j<6; j++){
            lotto[j] = deck[j]; //앞 6개만 로또 방에 넣어줌.
        }   
    }


    void order() { //정렬
        boolean b = true;
        while(b) {
            b=false;    
            for(int i =0; i<lotto.length-1; i++) {

                if( lotto[i] > lotto[i+1]) { // 뒤숫자보다 앞이 크면
                    int temp = lotto[i];
                    lotto[i] = lotto[i+1];
                    lotto[i+1] = temp; //자리바꿈.
                    b = true;
                }
            }

        }
    }

    /*
         주말숙제
                    int [] lotto= lm.make(); // lotto값 [6, 13, 25, 29, 31, 45]
                    lm.printPaper(lotto); 실행 시

                    출력 값:
                    01 02 03 04 05 ** 07
                    08 09 10 11 12 ** 14
                    15 16 17 18 19 20 21
                    22 23 24 ** 26 27 28
                    ** 30 ** 32 33 34 35
                    36 37 38 39 40 41 42
                    43 44 **

     */

    int[] printPaper(int[] lotto){
            for (int i=0; i<deck.length; i++) { // 1- 45 비교해서찍기
                for (int j=0; j<lotto.length; j++) {
                    //1. (1-45) -lotto 6개 숫자와 비교.
                    if((i+1)==lotto[j] && (i+1)%7==0) {// 숫자 일치하면서 7배수면 별찍고 줄바꿈.
                        System.out.print("** ");
                        System.out.println();
                        i++; //별로 이번 i값 대체..
                        //System.out.printf("[count %d]",count);
                    }else if ((i+1)==lotto[j]) {//숫자 일치하면 별찍기 .
                        System.out.print("** ");
                        i++; //별로 이번 i값 대체.. 즉 ++
                    }
                        
                }//비교 for문 끝.
                
                
                if((i+1)/10==0 && (i+1)%7!=0) { // 1자리수면 0붙이기 ..
                    System.out.print("0"+(i+1)+" ");
                }else if((i+1)%7==0 && (i+1)/10==0) { //7의 배수이고 한자리수면 0+줄바꿈
                    System.out.println("0"+(i+1)+" ");
                }else if((i+1)%7==0) { //7의 배수이면 줄바꿈
                    System.out.println((i+1)+" ");
                }
                else
                    System.out.print((i+1)+" ");

                
            } //for문끝.
            
            
        System.out.println();
        return this.lotto;
    }
    
    int[] make() {
    
        initialization();
        mix();
        extract();
        order();
    
        return lotto;
    }

}



출력결과

[2, 3, 4, 12, 21, 32]


01 ** ** ** 05 06 07 

08 09 10 11 ** 13 14 

15 16 17 18 19 20 ** 

22 23 24 25 26 27 28 

29 30 31 ** 33 34 35 

36 37 38 39 40 41 42 

43 44 45 


반응형