import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
public class Rand extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField jtf;
int[] lotto;
String st;
public Rand() {
for(int i =0;i<6;i++)
jtf = new JTextField(10);
Container ct = getContentPane();
JButton jb = new JButton("실행");
ct.setLayout(new GridLayout(2,1));
for(int i=0;i<6;i++)
ct.add(jtf);
ct.add(jb);
jb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
lotto = Randcell();
st = "";
for(int j = 0; j<lotto.length;j++){
if(j==0)
st=st + Integer.toString(lotto[j]);
else if(j<lotto.length)
st=st +", " + Integer.toString(lotto[j]);
}
jtf.setText(st);
}
public int[] Randcell() {
int[] res = {0,0,0,0,0,0};
int i;
for(i = 0; i<6; i++){
res[i.] = (int)((Math.random()*100)%45 + 1);
}
i = 0;
while(res[0]==res[1]|res[0]==res[2]|res[0]==res[3]|res[0]==res[4]|res[0]==res[5]|
res[1]==res[2]|res[1]==res[3]|res[1]==res[4]|res[1]==res[5]|
res[2]==res[3]|res[2]==res[4]|res[2]==res[5]|
res[3]==res[4]|res[3]==res[5]|
res[4]==res[5]){
if(i==6) i=0;
res[i.] = (int)((Math.random()*100)%45 + 1);
i++;
}
Arrays.sort(res);
return res;
}
public static void main(String[] args){
Rand rd = new Rand();
rd.setTitle("로또 번호 생성 v" + serialVersionUID);
rd.setSize(500,100);
rd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rd.setVisible(true);
}
}
1부터 46까지중에 무작위로 번호 6개를 추출해줌