A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© helloxiaoyu 中级黑马   /  2015-8-7 23:01  /  685 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Random;
class CardTest{
    public static void main(String[] args){
        FaPaiJi fpj = new FaPaiJi();
        Card c0 = fpj.getCard();
        Card c1 = fpj.getCard();
        Card c2 = fpj.getCard();
        Card[] cards = new Card[3];
        cards[0] = c0;
        cards[1] = c1;
        cards[2] = c2;
        for( int x=0; x<cards.length; x++){
            System.out.println(cards[x].getHuase()+""+cards[x].getDianshu()+"");
        }
    }
}
class Card {
            //成员变量
    String huase;//花色
    int dianshu;//点数
            //构造方法
    Card(){}
    Card(String huase, int dianshu){
        this.huase = huase;
        this.dianshu = dianshu;
    }
    public String getHuase(){
        return huase;
    }
    public int getDianshu(){
        return dianshu;
    }
}
class FaPaiJi {
//            成员方法  
//            返回值类型
//            参数列表
    public Card getCard(){
                //创建对象
       String[] hs = {"黑桃","梅花","方块","红桃"};
        Random r = new Random();
        int x = r.nextInt(4);
        String huase = hs[x];

        Card card = new Card(huase,dianshu);
        return card;
    }
}
输出结果可能出现相同项,怎样解决这个问题?

/*
    生成牌的时候,直接生成52不一样的牌
    随机 Random().nextInt(52),返回的是 0--51
    只要随机数不一样
    */
        Random r = new Random();
        int x1 = r.nextInt(13)+1;
        int x2 = r.nextInt(13)+14;
        int x3 = r.nextInt(13)+27;
        int x4 = r.nextInt(13)+40;
        String[][] hs = {"黑桃","梅花","方块","红桃"};
后面怎么做?求解

评分

参与人数 1黑马币 +5 收起 理由
尛子 + 5 很给力!

查看全部评分

8 个回复

倒序浏览

使用集合

import java.util.ArrayList;
import java.util.Collections;

public class D {
        public static void main(String[] args) {
                ArrayList<String> array = new ArrayList<String>();
                // 定义一个花色数组
                String[] colors = { "♠", "♥", "♣", "♦" };
                // 定义一个点数数组
                String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10","J", "Q", "K" };
                for (String color : colors) {
                        for (String number : numbers) {
                                array.add(color.concat(number));
                        }
                }
                // 洗牌
                Collections.shuffle(array);
                for (int i = 0; i < 3; i++) {
                        System.out.println(array.get(i));
                }
        }
}
回复 使用道具 举报
谢谢分享,学习了
回复 使用道具 举报
import java.util.Random;

public class C {

        public static void main(String[] args) {
                Random random = new Random();
                int a[] = new int[4];
                for (int i = 0; i < a.length; i++) {
                        //生成一个介于1到52的数字
                        a[i] = random.nextInt(52)+1;
                        for (int j = 1; j < i; j++) {
                                //如果重复,退回去重新生成随机数
                                while (a[i] == a[j]) {
                                        i--;
                                }
                        }
                }
                       
                for (int i = 1; i < a.length; i++) {
                        if (a[i]<14&&a[i]>0) {
                                System.out.println("红桃"+a[i] +"  ");
                        }else if (a[i]<27&&a[i]>13) {
                                System.out.println("方块"+(a[i]-13) +"  ");
                        }else if (a[i]<40&&a[i]>26) {
                                System.out.println("黑桃"+(a[i]-26) +"  ");
                        }else {
                                System.out.println("梅花"+(a[i]-39) +"  ");
                        }
                }

        }

}
回复 使用道具 举报
import java.util.HashSet;
import java.util.Random;

public class B {
public static void main(String[] args) {
               
                //创建一个Set集合
                HashSet<Integer> hs = new HashSet<Integer>();
               
                //向hashset中不停的存放随机数,当该集合的长度大于3就不再放了
                while(hs.size()<3) {
                        //生成随机数
                        int randomNumber = new Random().nextInt(52)+1;
                        hs.add(randomNumber);
                        if (randomNumber<14&&randomNumber>0) {
                 System.out.println("红桃"+randomNumber +"  ");
                        }else if (randomNumber<27&&randomNumber>13) {
                 System.out.println("方块"+(randomNumber-13) +"  ");
                        }else if (randomNumber<40&&randomNumber>26) {
                 System.out.println("黑桃"+(randomNumber-26) +"  ");
                        }else {
                 System.out.println("梅花"+(randomNumber-39) +"  ");
                        }
                }
                System.out.println(hs);
}
}
回复 使用道具 举报
嘿,这么巧,我昨天刚做了个炸金花小程序,我感觉最关键还是现实中抽一张牌就不会再抽出来了,但是计算机会出现重复的牌,需要判断再抽牌,若是重复就让他继续随机抽。下面是我的小程序:欢迎交流指正!
import java.util.Random;
class PukeArray1
{
public static void main(String[] args) {
//为了保证不重复并且好判断,就使用二维数组,方法比较笨点
  String[][] puke = {{"A","2","3","4","5","6","7","8","9","10","J","Q","K"},
                              {"A","2","3","4","5","6","7","8","9","10","J","Q","K"},
                              {"A","2","3","4","5","6","7","8","9","10","J","Q","K"},
                              {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}};
for(int x=0;x<10;x++){
  Random ra = new Random();
  int hua1 = ra.nextInt(4);
  int shu1 = ra.nextInt(13); //此处,一维数组的索引应该由0-12;
  int hua2 = ra.nextInt(4);
  int shu2 = ra.nextInt(13);  
  int hua3 = ra.nextInt(4);
  int shu3 = ra.nextInt(13);
  while((hua1*100+shu1)==(hua2*100+shu2)|(hua1*100+shu1)==(hua3*100+shu3)| (hua2*100+shu2)==(hua3*100+shu3))//判断有没有完全相同的牌。用"|"判断,有一个相等就不行,因为
  {           //现在乘积都完全保证不会有重复,一旦有一个重复,一定是完全一样。
   hua1 = ra.nextInt(4);
   shu1 = ra.nextInt(13);   
   hua2 = ra.nextInt(4);
   shu2 = ra.nextInt(13);   
   hua3 = ra.nextInt(4);
   shu3 = ra.nextInt(13);
  }
  getType(hua1);
  System.out.print(puke[hua1][shu1]+"   ");
  getType(hua2);
  System.out.print(puke[hua2][shu2]+"   ");
  getType(hua3);
  System.out.print(puke[hua3][shu3]+"   ");
  System.out.println();
  
}


}

public static void getType(int x)//随机花色。
{
  switch(x)
  {
   case 0:
    System.out.print("红桃");
    break;
   case 1:
    System.out.print("方块");
    break;
   case 2:
    System.out.print("梅花");
    break;
   case 3:
    System.out.print("黑桃");
    break;
   default:
    System.out.println("错误花色");
    break;
  }
}
}
回复 使用道具 举报
MengYa 中级黑马 2015-8-20 23:47:28
7#
赞!!!!!!!!!!!!!!!!!!!!!!!!!!!!
回复 使用道具 举报
发牌可以使用模运算,如果3个人, 扑克总数与3取模, 结果为0,1,2
回复 使用道具 举报
赞!!!!!!!!!!!!!!!!!!!!!!!!!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马