zoukankan      html  css  js  c++  java
  • 随机生成n张扑克牌。

    • 开发提示:

      • 使用集合保存所有的扑克牌对象。
      • 从所有牌中,随机移除n张牌,保存到新集合。
      • 判断n的值,不能超越一副扑克牌的范围。
    • 参考答案:

    	public class Test6 {
    	    public static void main(String[] args) {
    	        int n = 5;
    	        ArrayList<Card> cards = randomCard(n);
    	
    	        if (cards != null) {
    	            System.out.println("随机"+ n +"张牌:" );
    	            for (int i = 0; i < cards.size(); i++) {
    	                Card card = cards.get(i);
    	                card.showCard();
    	            }
    	        }else {
    	            System.out.println(n+"超越范围,无法获取牌" );
    	        }
    	
    	        System.out.println();
    	        System.out.println();
    	        int n2 = 55;
    	        ArrayList<Card> cards2 = randomCard(n2);
    	
    	        if (cards2 != null) {
    	            System.out.println("随机"+ n2 +"张牌:" );
    	            for (int i = 0; i < cards.size(); i++) {
    	                Card card = cards.get(i);
    	                card.showCard();
    	            }
    	        }else {
    	            System.out.println("随机"+ n2 +"张牌:
    超越范围,无法获取" );
    	        }
    	    }
    	    public static ArrayList<Card> randomCard(int n) {
    	        if (n > 54 || n < 0)
    	            return null;
    	
    	        ArrayList<Card> rList = new ArrayList<>();
    	        ArrayList<Card> cards = allCard();
    	
    	        Random r = new Random();
    	        for (int i = 0; i < n; i++) {
    	            int index = r.nextInt(cards.size());
    	            Card rCard = cards.remove(index);//我认为全文最高明的地方在此(52张牌做到没有重复使用,且不用写那么多避免重复的代码),当remove掉一个时,返回值add到rList中,size随之减小
    	            rList.add(rCard);
    	        }
    	        return rList;
    	    }
    	
    	    public static ArrayList<Card> allCard() {
    	        ArrayList<Card> allList = new ArrayList<>();
    	        // 花色数组
    	        String[] hs = {"黑桃", "红桃", "梅花", "方片"};
    	        // 点数数组
    	        String[] ds = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    	
    	        for (int H = 0; H < hs.length; H++) {
    	            for (int d = 0; d < ds.length; d++) {
    	                Card card = new Card(hs[H], ds[d]);
    	                // 添加到集合
    	                allList.add(card);
    	            }
    	        }
    	        return allList;
    	    }
    	}
    	
    	class Card {
    	    private String ds; // 点数
    	    private String hs; // 花色
    	    public Card(String ds, String hs) {
    	        this.ds = ds;
    	        this.hs = hs;
    	    }
    	    public void showCard() {
    	        System.out.print(ds + hs+" ");
    	    }
    	}
    
  • 相关阅读:
    视频直播架构
    day1 python 入门
    python 多用户登录
    mysql innobackup 备份脚本
    ADT离线安装
    真机调试adb:wait for device 解决方案
    php中的魔术方法
    整理资料
    PostgreSQL表空间_数据库_模式_表_用户角色之间的关系[转]
    PHP获取文件夹的大小
  • 原文地址:https://www.cnblogs.com/zx-coder/p/12798099.html
Copyright © 2011-2022 走看看