当前位置: 代码迷 >> J2ME >> 用数组,写个洗牌的步骤
  详细解决方案

用数组,写个洗牌的步骤

热度:5174   发布时间:2013-02-25 21:31:34.0
用数组,写个洗牌的方法
public class Card{
public static void main(String[] args) {
  int[] p=new int[54];

for(int i=1;i<54;i++){

  p[i]=i; 

double b=Math.random()*50+1;

  int c=(int)b;

  if(c>=i){

  

  int temp=p[i];

  p[i]=p[p.length-1];

  p[p.length-1]= temp;

  

  }

  System.out.println(p[i]);

  }
  }
}
怎样才能输不出来0

------解决方案--------------------------------------------------------
我说的这个方法。。。已经非常简单了吧?

Java code
// 初始化牌盒int[] p= new int[54];// 初始化所有扑克for(int i=0;i<54;i++) p[i] = i+1; // 顺序洗牌for(int i=0;i<54;i++) {    // 随机跟另一张牌交换    int r = (int)(Math.random()*54);    // 交换    int tmp = p[r];    p[r] = p[i];    p[i] = tmp;}// 至此完毕
  相关解决方案