本帖最后由 白堇翎 于 2013-3-11 00:06 编辑
直接写一个数组装所有的牌
写另外一个数组装洗过的牌
这样会不会更加简单..
======================
因为小弟才刚刚开始学java 还没有学到随机这里 刚刚研究了下Random的用法 得出了以下算法- import java.util.Random;
- class test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String[] puker={"黑桃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"};
- //定义一个数组 装下所有的52张牌
- String[] puker_2=new String[52];
- //创建一个随机数组
- Random rand=new Random();
- for(int a=0;a<puker_2.length;a++){
- puker_2[a]= puker[rand.nextInt(puker.length)];
- System.out.print(puker_2[a]);
- }
- }
- }
复制代码 但是存在缺陷是会出现重复的牌 目前正在想办法解决..
=======================================================- import java.util.Random;
- class test {
- public static void main(String[] args) {
- String[] puker={"黑桃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"};
- //定义一个数组 装下所有的52张牌
- int[] Arr = new int[52];//定义一个整数类型的数组 用于获取puker数组的下标 长度与puker数组的长度相等
- for (int i = 0; i < Arr.length; i++) {
- boolean flag = false;//这里定义一个布尔型变量 用于判断下面是否应该赋值
- do {
- flag = false;
- int num = (int) (Math.random() * 52 + 0);//产生一个0~52之间的随机值
- for (int j = 0; j < i; j++) {//这里使用双层嵌套来确保数字不重复 用是否来判断是否相等 假如相等则不会执行赋值的语句
- if (num == Arr[j]) {
- flag = true;
- break;
- }
- }
- if (!flag) {
- Arr[i] = num;//把不相等的值赋给Arr 注意此处是赋给角标为i的数字 也就是说从0开始一直到51都不会出现重复的值
- }
- }
- while (flag);
- System.out.print(puker[Arr[i]]+","); //打印出该值在数组puker中对应的下标对应的具体值
- }
- }
- }
复制代码 总结
先得出一个由0~51组成的的总长度为52,不重复的数组
再用其具体值打印出对应的puker数组中相对应的下标的具体值..- import java.util.Random;
- class test {
- public static void main(String[] args) {
- String[] puker={"黑桃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"};
- String[] puker_2 = new String[52];
- //定义一个数组 装下所有的52张牌
- int[] Arr = new int[52];//定义一个整数类型的数组 用于获取puker数组的下标 长度与puker数组的下标相等
- for (int i = 0; i < Arr.length; i++) {
- boolean flag = false;//这里定义一个布尔型变量 用于判断下面是否应该赋值
- do{
- flag = false;
- int num = (int)(Math.random() * 52 + 0);//产生一个0~52之间的随机值 这里第一次编译竟然提示我损失精度 果断强转
- for (int j = 0; j < i; j++) {//这里使用双层嵌套来确保数字不重复
- if (num == Arr[j]) {
- flag = true;
- break;//相等就跳出 重新执行
- }
- }
- if (!flag) {
- Arr[i] = num;//把不相等的值赋给Arr 注意此处是赋给角标为i的数字 也就是说从0开始一直到51都不会出现重复的值
- }
- }
- while (flag);
- puker_2[i]=puker[Arr[i]];
- }
- for (int x=0;x<(puker_2.length)/3 ;x++ )
- {
- System.out.print((puker_2[x])+" "+"\t");
- System.out.print((puker_2[x+17])+" "+"\t");
- System.out.println((puker_2[x+34])+" "+"\t");
- }
- System.out.println("======================");
- System.out.println("开始斗地主");
-
- }
复制代码 睡觉之前把数组分成三份 求高人实现斗地主功能 |