2008년 8월 6일 수요일

java Random





import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class Coin implements ActionListener {
static JLabel label;
public static void main(String[] args){
JFrame frame = new JFrame("Coin Program");
frame.setPreferredSize(new Dimension(200,120));
frame.setLocation(500,400);
Container contentPane = frame.getContentPane();
JButton button = new JButton("코인 던지기");
label = new JLabel(" ");
contentPane.add(button,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
Coin listener = new Coin();
button.addActionListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int f=0,b=0;
Random oRandom = new Random();
for(int i= 0;i<77;i++){
// 1~77까지의 정수를 랜덤하게 출력
int r = oRandom.nextInt(77) + 1;
//앞뒤판단
if(r%2==0)f++;
else b++;

}
label.setText("앞: "+f+"뒷: "+b);
}
}

//의문점 쪽지 주세요

댓글 없음:

댓글 쓰기

-


Sidewinder


World


FishMusic


LaughingBaby