본문 바로가기

Coding/Java

로또 번호 생성기!

import java.util.Arrays;
import java.util.Scanner;

public class LottoTest {

    public static void main(String[] args) {

        while(true) {

            Scanner scan = new Scanner(System.in);
            System.out.println("입력한 숫자만큼 수량이 출력 됩니다.");
            String inner = scan.nextLine();

            try {

               int input = Integer.parseInt(inner);
               lottoStart(input);

            } catch (Exception e) {

               System.out.println("숫자값만 입력해주세요.");

            }

        }
    }

    //로또 출력이 몇번 될 것인가
    public static void lottoStart(int count) {
        if(count < 1) {
           System.out.println("숫자는 1보다 커야 합니다.");
        } else {
            int[] lotto = new int[6];
            for(int i=0; i<count; i++) {
                for(int j=0; j<6; j++) {
                    int selector = randomInt(lotto);
                    lotto[j] = selector;
                }
                Arrays.sort(lotto);
                System.out.println(Arrays.toString(lotto));
            }
        }
    }

    //중복되지 않는 숫자값 가져오기
     public static int randomInt(int[] tmp) {
        boolean check = false;
        int result = 0;

        while(!check) {
            //랜덤으로 값 추출
            boolean repeat = false;
            result = (int)(Math.random()*45+1);
            //기존에 값과 일치 여부 확인
            for(int i=0; i<tmp.length; i++) {
                if(tmp[i] == result) {
                    repeat = true;
                }
            }
            //일치하는 값이 없을경우 while문 종료
            if(!repeat) {
                check = true;
            }
        }
        return result;
    }
}

 

 

 

'Coding > Java' 카테고리의 다른 글

블랙잭 2차  (0) 2020.05.05
블랙잭 초안  (0) 2020.05.03
java.lang.ArithmeticException: / by zero  (0) 2020.01.02
OOP  (0) 2018.02.23
별찍기  (0) 2017.12.19