Java2009. 3. 17. 14:34

class Lotto {
    public static void main(String[] args) {
        Lotto lotto = new Lotto();
        for(int i=0; i<100; i++)
            System.out.print(lotto.getLottoNum() + ((i+1)%5==0?"\n":"\t"));
    }

    public String getLottoNum() {
        StringBuffer sb = new StringBuffer();
        int[] num = this.createLottoNum();
        num = this.sort(num);
        for(int i:num) {
            if(i<10) sb.append("0" + Integer.toString(i) + " ");
            else sb.append(Integer.toString(i) + " ");
        }
        return sb.toString();
    }

    //get random number(6place within 1 to 45)
    private int[] createLottoNum() {
        int[] num = new int[6];
        for(int i=0; i<num.length; i++) {
            num[i] = (int)((Math.random()*45)+1);
            for(int j=0; j<i; j++)
                if(num[i] == num[j]) {
                    i--;
                    break;
                }
        }
        return num;
    }

    //bubble sort
    private int[] sort(int[] num) {
        int tmp;
        for(int i=0; i<num.length-1; i++) {
            for(int j=0; j<num.length-i-1; j++) {
                if(num[j] > num[j+1]) {
                    tmp = num[j];
                    num[j] = num[j+1];
                    num[j+1] = tmp;
                }
            }
        }
        return num;
    }
}

'Java' 카테고리의 다른 글

Base64 encode & decode in Java  (0) 2009.03.17
자바 랜덤 문자열 생성  (0) 2009.03.10
JSTL forEach forToken  (0) 2008.10.02
스타시디키 checksum Java & C# 소스  (0) 2008.09.26
Posted by Main()