[메인 ]


public class Main {


public static void main(String[] args) {

new LottoNumber();

}


}






[랜덤번호뽑기]


public class Ran {


private int ran;


// 랜덤한 숫자를 출력할 1-45까지 출력

public int random() {


double ran = Math.random();

ran = ((ran * 100) % 45) + 1;

// ran에 값을 출력하시고

// ran이 0.5가 나왔을 때


return (int) ran;

}


public int getRan() {

return ran;

}


}





[Frame ]

package Random;


import java.awt.Button;

import java.awt.Frame;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.HashSet;

import java.util.Set;



public class LottoNumber extends Frame {

Set<Integer> b = new HashSet<Integer>();

public LottoNumber() {

ArrayList_output sa = new ArrayList_output();

setTitle("나의 첫 로또번호");

// 버튼 인스턴스

Button btn = new Button("번호 뽑기 ");

Button btn2 = new Button("출력");

// 패널 인스턴스

Panel pa = new Panel();

// 패널에 버튼 추가

pa.add("center", btn);

pa.add("center", btn2);

// pa 추가

add(pa);


// 이벤트 버튼

class ClassBtnLotto implements ActionListener {


@Override

public void actionPerformed(ActionEvent e) {

b =sa.addArray();

System.out.println("이벤트 " +b);

}


}

// 이벤트 버튼

ClassBtnLotto select = new ClassBtnLotto();

btn.addActionListener(select);


// 이벤트 버튼 2

class ClassBtnLotto2 implements ActionListener {


@Override

public void actionPerformed(ActionEvent e) {

System.out.println("눌렸는가");

for(Object is : b) {

System.out.println(is);

}


}


}

// 이벤트 버튼

ClassBtnLotto2 select2 = new ClassBtnLotto2();

btn2.addActionListener(select2);


// 화면 보여주기

setVisible(true);

setBounds(400, 400, 400, 400);

}

}





[랜덤한 숫자를 뽑아서 하나씩 값을 추가 ]

package Random;


import java.util.ArrayList;

import java.util.HashSet;

import java.util.Set;



public class ArrayList_output {

// 6번을 넘기지 않을 변수

private int a;


// 배열값을 저장하기 위한 메소드

Set<Integer> ar = new HashSet<Integer>();


public Set addArray() {

// 랜덤인스턴스 호출

Ran r = new Ran();

// 랜덤숫자를 받아온다.

int a = r.random();

ar.add(a);

System.out.println(a + "번호를 뽑았습니다.");


return ar;

}

}





+ Recent posts