随机的抽出一副牌中的三张。

 在一副牌中,随机的抽出三张牌,这三张牌不能够重复。

import javax.swing.*;
import java.awt.*;

public class CardGame extends JFrame {
	ImageIcon[] icons = new ImageIcon[3];
	JLabel[] lbs = new JLabel[3];
	
	public CardGame() {
		int[] a = new int[3];
		
		//产生三个不重复的随机数,每个数字对应着每一张牌
		for(int i = 0; i < a.length; i++) {
			int x = (int)(1 + Math.random() * 52);
			for(int j = 0; j < i; j++) {
				if(a[j] == x) {
					x = (int)(1 + Math.random() * 52);
				}
			}
			a[i] = x;
		}

		for(int i = 0; i < lbs.length; i++) {
			icons[i] = new ImageIcon("image/card/" + a[i] + ".png");
			lbs[i] = new JLabel(icons[i]);
		}
		setLayout(new GridLayout(1, 3, 1, 1));
		add(lbs[0]);
		add(lbs[1]);
		add(lbs[2]);
	}
	
	public static void main(String[] args) {
		JFrame frame = new CardGame();
		frame.setTitle("扑克游戏");
		frame.pack();
		frame.setLocation(300, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值