본문 바로가기

자작 프로그램

자바로 구현한 로또 번호 생성 프로그램

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Lotto.java 소스


package com.tapito;

import java.util.*;

/**
 * 자동 번호 생성 클래스입니다.
 * @author tapito
 *
 */
public final class Lotto
{
	private Random random;
	private byte[] numbers = null;
	private byte bonus = 0;
	
	/**
	 * Lotto 클래스 형식 인스턴스를 초기화합니다.
	 */
	public Lotto()
	{
		this.numbers = new byte[6];
		this.bonus = 0;
		this.random = new Random();
		
		this.makeNumbers();
	}
	
	/**
	 * 생성된 숫자를 오름차순 정렬합니다.
	 */
	private void sortNumbers()
	{
		byte t = 0;
		
		for(int i = 0; i < this.numbers.length - 1; i++)
		{
			for(int j = i + 1; j < this.numbers.length; j++)
			{
				if(this.numbers[i] > this.numbers[j])
				{
					t = this.numbers[i];
					this.numbers[i] = this.numbers[j];
					this.numbers[j] = t;
				}
			}
		}
	}
	/**
	 * 자동 숫자를 생성하여 클래스를 갱신합니다.
	 */
	public void makeNumbers()
	{
		byte n = 0;
		
		for(int i = 0; i < 6; i++)
		{
			do
			{
				n = (byte)(this.random.nextInt(44) + 1); // 0부터 44까지 임의의 수 생성 후 1 더한다.
			} while(this.exists(n)); // 중복되는 숫자라면 다시 생성
			
			this.numbers[i] = n;
		}
		
		this.sortNumbers();
		
		do
		{
			n = (byte)(this.random.nextInt(44) + 1);
		} while(this.exists(n));
		this.bonus = n; // 보너스 숫자.
	}
	
	/**
	 * 지정된 숫자의 존재 여부를 검사하여 중복 숫자를 방지합니다.
	 * @param n 검사할 숫자입니다.
	 * @return n이 존재하면 true, 그렇지 않으면 false를 반환합니다.
	 */
	public boolean exists(byte n)
	{
		boolean ret = false;
		
		for(byte t : this.numbers)
		{
			if(t == n)
			{
				ret = true; break;
			}
		}
		
		return ret;
	}
	
	/**
	 * n번째 숫자를 가져옵니다.
	 * @param n 가져올 숫자입니다. 1부터 6까지로 제한되며 보너스 숫자는 7입니다.
	 * @return n번째에 해당되는 숫자입니다.
	 * @throws Exception n이 범위를 초과하면 예외를 throw합니다.
	 */
	public byte getNumber(int n) throws Exception
	{
		byte ret = 0;
		
		if(n > 7) throw new IndexOutOfBoundsException();
		if(n < 1) throw new IndexOutOfBoundsException();
		
		if((1 <= n) && (n <= 6))
			return this.numbers[n - 1];
		else
			return this.bonus;
	}
}

Program.java 소스


/********************************
 * 로또 번호 생성 프로그램        *
 * Program.java                 *
 * main 메서드를 정의한다.         *
 *              (c) tapito 2012 *
 ********************************/
package com.tapito;

public class Program
{
	public static void main(String[] args) throws Exception
	{
		Lotto lotto = new Lotto();
		int getc;

		System.out.println("Tapito(R) Lotto Generator");
		System.out.println("(c) tapito : http://tapito.tistory.com/");
		
		
		do
		{
			lotto.makeNumbers();
			
			System.out.println();
			System.out.printf("%2d, %2d, %2d, %2d, %2d, %2d ...... %2d\n",
				lotto.getNumber(1),
				lotto.getNumber(2),
				lotto.getNumber(3),
				lotto.getNumber(4),
				lotto.getNumber(5),
				lotto.getNumber(6),
				lotto.getNumber(7));
			
			do
			{
				System.out.print("반복 = y, 종료 = n >> ");
				getc = System.in.read();
				System.in.read(); System.in.read();
			} while(!((getc == (int)'y') || (getc == (int)'n') || (getc == (int)'Y') || (getc == (int)'N')));
		} while(getc != (int)'n');
		
		System.out.println();
		System.out.println("Bye~");
		System.out.println("- by tapito");
		return;
	}
}

TapitoLotto.java JAR 파일


실행 결과