java洗牌
㈠ 怎么写一个java程序洗牌后重新排序
importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;
importjava.util.Random;
/**
*@Date:2013-10-22上午10:38:03<br/>
*@authorLisj
*@version
*@sinceJDK1.6
*/
publicclassPokerOrder{
//生成一副牌有顺序
publicList<Poker>createPokers(){
List<Poker>pokers=newArrayList<Poker>();
for(inti=0;i<13;i++){
for(intj=0;j<4;j++){
pokers.add(newPoker(i+1,j+1));
}
}
pokers.add(newPoker(14,1));
pokers.add(newPoker(14,2));
returnpokers;
}
//洗牌打乱顺序
publicList<Poker>shuffle(List<Poker>pokers){
List<Poker>newPokers=newArrayList<Poker>();
Randomrandom=newRandom();
for(inti=pokers.size()-1;i>0;i--){
intk=random.nextInt(i+1);
newPokers.add(pokers.get(k));
pokers.remove(k);
}
newPokers.add(pokers.get(0));
returnnewPokers;
}
publicList<Poker>order(List<Poker>pokers){
returnpokers;
}
publicstaticvoidmain(String[]args){
PokerOrderpokerOrder=newPokerOrder();
List<Poker>pokers=pokerOrder.createPokers();
for(inti=0;i<pokers.size();i++){
System.out.print(pokers.get(i).getNumber()+"-"+pokers.get(i).getColor()+"");
}
System.out.println();
//Collections.shuffle(pokers);
pokers=pokerOrder.shuffle(pokers);
for(inti=0;i<pokers.size();i++){
System.out.print(pokers.get(i).getNumber()+"-"+pokers.get(i).getColor()+"");
}
System.out.println();
Collections.sort(pokers,newComparator<Poker>(){
publicintcompare(Pokero1,Pokero2){
returno1.compareTo(o2);
}
});
for(inti=0;i<pokers.size();i++){
System.out.print(pokers.get(i).getNumber()+"-"+pokers.get(i).getColor()+"");
}
System.out.println();
}
}
classPoker{
//1~13,14大小
privateintnumber;
//number1~13时,1:黑桃,2:梅花,3:红桃,4:方片.14时,1:小王,2:大王
privateintcolor;
publicPoker(intnumber,intcolor){
this.number=number;
this.color=color;
}
publicintcompareTo(Objectarg0){
PokertmpPoker=(Poker)arg0;
if(this.number==tmpPoker.number){
if(this.color>tmpPoker.color){
return1;
}else{
return-1;
}
}else{
if(this.number>tmpPoker.number){
return1;
}else{
return-1;
}
}
}
publicintgetNumber(){
returnnumber;
}
publicintgetColor(){
returncolor;
}
publicvoidsetNumber(intnumber){
this.number=number;
}
publicvoidsetColor(intcolor){
this.color=color;
}
}
㈡ 如何洗牌在Java二维数组
上面的代码除去语法的问题不说,基本没有上下文,所以完全看不出是哪出了问题。请你把代码贴全才能帮你解答问题,好吗?
因为j在i=0的外层循环中已经到了最大值,于是i=1时j的循环不会执行。
所以以后千万别犯这个错,最好是在for里面声明循环变量。
用下面的代码就可以了。
Scannerin=newScanner(System.in);
inta=in.nextInt();
String[][]b=newString[2*a+1][2*a+1];
for(inti=0;i<2*a+1;i++){
for(intj=0;j<2*a+1;j++){
b[i][j]="*";
}
}
for(inti=0;i<2*a+1;i++){
for(intj=0;j<2*a+1;j++)
System.out.print(b[i][j]);
System.out.println();
}
㈢ 用java ArrayList集合编写扑克牌洗牌与发牌
import java.util.ArrayList;
import java.util.Random;
public class PokerDemo {
ArrayList<Card> poker = new ArrayList<Card>();
Player[] players;
int n = 4;
String[] name = {"Wang", "Zhang", "Li", "Yuan"};
public PokerDemo() {
players = new Player[n];
for(int i = 0; i < players.length; i++)
players[i] = new Player(name[i]);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 13; j++)
poker.add(new Card(i, j));
System.out.println("洗牌前: " + poker);
xipai();
System.out.println("洗牌后: " + poker);
fapai();
System.out.println("发牌后: ");
for(int i = 0; i < players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayList<Card> tmp = new ArrayList<Card>();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayList<Card>(tmp);
}
public void fapai() {
int p = 0;
while(!poker.isEmpty()) {
Card card = poker.get(0);
players[p].add(card);
p = (p + 1) % n;
poker.remove(0);
}
}
public static void main(String[] args) {
new PokerDemo();
}
}
class Card {
int val, sty;
final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] style = {"黑桃", "红心", "梅花", "方块"};
public Card(int s, int v) {
sty = s;
val = v;
}
public String toString() {
return style[sty]+ " " + value[val];
}
}
class Player {
ArrayList<Card> cards = new ArrayList<Card>();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
㈣ java 中如何实现一副扑克牌洗牌的功能。如果无法书写完整的代码,可以用文字描述出自己的实现思路
1.定义牌类
public class Card{
public int id;//牌大小
public String number;//牌值名称
public String color;//花色
}
2.定义牌组,用ArrayList存放所有牌
ArrayList<Card> cardGroup =new ArrayList<Card>();
3.定义花色枚举
public static final String[] COLORS=new String[]{"方块","梅花","红桃","黑桃"};
4.定义牌值大小枚举
public static fina String[] CARDVALUES=new String[]{"A","1","2","3",
"4","5","6","7","8","9","10","J","Q","K","King_Small","King_Big"};
5.初始化牌组,这就是一副新牌,没切过,己经排好序的
public void initCardGroup(){
//加入A~K
for(int i=0;i<13;i++){
for (int j=0;j<4;j++){
//生成一张牌的对象
Card card=new Card();
card.id=i;
card.color=COLORS[j];
card.munber=CARDVALUES[i];
//将对象加入牌组
cardGroup.add(card);
}
}
//加入大小王
Card card_k1=new Card();
card_k1.id=13;
card_k1.color="King_Small";
card_k1.number="King_Small";
cardGroup.add(card_k1);
Card card_k2=new Card();
card_k2.id=14;
card_k2.color="King_big";
card_k2.number="King_big";
cardGroup.add(card_k2);
}
6. 洗牌,将原牌组打乱,得到新的牌组
public ArrayList<Card> flushCardsGroup(ArrayList<Card> src){
ArrayList<Card> result=new ArrayList<Card>();
while(src.size>0){
int size=src.size();
//以size为限,生成0~size之间的随机数
Random r=new Random();
int index=r.nextInt(size);
//对index做个检查
if(index>0){index=0;}
if(index>size-1){index=size-1;}
//index就是捡出来的牌,加到新的数组中,从原来数组中删除
Card card=src.get(index);
result.add(card);
card.remove(index);
}
return result;
}
㈤ java洗牌算法问题
你用System.out.print方法打印一个对象时,控制台会默认调用其toString方法,java就这么设计的
㈥ 关于洗牌算法,请用JAVA编写,定义一个数组,储存1-52以内的数,打乱顺序输出!
import java.util.Enumeration;
import java.util.Hashtable;/**
* 7. * 乱序扑克牌 洗牌方法 8. * 9. *
*
* @author virture 10. * 11.
*/
public class Cards { Hashtable htMember = new Hashtable();// 放置扑克牌的Hash表 public Cards() { } public void put(String card) {
htMember.put(card, card);
} public void get() {
System.out.println("你拿到的牌是:");
Enumeration RLKey = htMember.keys();
while (RLKey.hasMoreElements()) {
String accKey = RLKey.nextElement().toString();// 取HashTable中的关键字词
System.out.print((String) htMember.get(accKey) + ",");
}
} public static void main(String[] args) {
String[] cards = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
String[] kinds = { "黑桃", "红桃", "梅花", "方块" }; Cards cardList = new Cards(); String suit;// 当前选中牌的类型
String face;// 当前选中牌
int randomCardNum = 52;// 当前随机取牌的个数,记住不能大于全部牌52张 while (true) {
suit = kinds[Math.round((float) Math.random() * (kinds.length - 1))];
face = cards[Math.round((float) Math.random() * (cards.length - 1))]; cardList.put(suit + face);
if (cardList.htMember.size() >= randomCardNum
&& cardList.htMember.size() <= 52) {
break;
}
}
cardList.get();
}
}
㈦ 请问用java数组编写洗牌程序怎么写我刚学java,请多指教,谢谢!!!
package xly04;
import java.util.Random;
public class ArrayDemo3 {
/**
* 数组应用:
* 1 随机洗牌
* 2 顺序发牌
*/
public static void main(String[] args) {
String[] cards=new String[]{"黑桃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","大王","小王"};
//Random random=new Random();.......(1)
//int index=random.nextInt(10);//[0,10) %10
//System.out.println(index);
//String card=cards[random.nextInt(cards.length)];//长度为8,随机下标[0,8)
//System.out.println(card);//随机的某张牌
for(int i=cards.length-1;i>=1;i--){
int j=new Random().nextInt(i);//或者(1)不注销用int j=random.nextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;i<cards.length;i++){
System.out.print(cards[i]+",");
}
System.out.println();//输出回车字符
//顺序发牌:顺序的将每个牌发给每个人
String[] players=new String[]{"马伊琍","文章","六六"};
//int i=0;
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
//System.out.println(players[i++%players.length]);
for(int i=0;i<cards.length;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每张扑克牌
String player=players[i%players.length];
//player代表每个玩游戏的人
System.out.println(player+":"+card);
}
}
}
㈧ java扑克牌洗牌程序,求抽可以抽出特定牌的次数
package;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.Scanner;
/**
*发牌游戏
*/
publicclassPokerGame
{
/**
*共有1幅扑克牌,不包括大王和小王共52张牌。
*/
publicstaticfinalintPOKER_NUMBER=52;
/**
*特殊分隔符_
*/
privatestaticfinalStringT="_";
/**
*52张牌<br/>
*顺序为黑桃、红心、草花、方块
*/
publicstaticfinalString[]POKERS=
{
"A"+T+"0","A"+T+"1","A"+T+"2","A"+T+"3",
"2"+T+"0","2"+T+"1","2"+T+"2","2"+T+"3",
"3"+T+"0","3"+T+"1","3"+T+"2","3"+T+"3",
"4"+T+"0","4"+T+"1","4"+T+"2","4"+T+"3",
"5"+T+"0","5"+T+"1","5"+T+"2","5"+T+"3",
"6"+T+"0","6"+T+"1","6"+T+"2","6"+T+"3",
"7"+T+"0","7"+T+"1","7"+T+"2","7"+T+"3",
"8"+T+"0","8"+T+"1","8"+T+"2","8"+T+"3",
"9"+T+"0","9"+T+"1","9"+T+"2","9"+T+"3",
"10"+T+"0","10"+T+"1","10"+T+"2","10"+T+"3",
"J"+T+"0","J"+T+"1","J"+T+"2","J"+T+"3",
"Q"+T+"0","Q"+T+"1","Q"+T+"2","Q"+T+"3",
"K"+T+"0","K"+T+"1","K"+T+"2","K"+T+"3"
};
/**
*原始的扑克牌
*/
publicstaticfinalString[]ORIAL_POKERS=POKERS.clone();
/**
*发到哪张牌0
*/
publicstaticintcurrentIndex=0;
/**
*四种花色
*/
publicstaticfinalStringPOKER_COLOR="黑桃"+T+"红心"+T+"草花"+T+"方块";
/**
*发牌
*@paramnint一共几个人玩牌
*@paramresults储存发牌结果的数组
*/
publicstaticvoiddrawPoker(intn,String[][]results)
{
intm=n-1;
if(n==0)
{
return;
}
intnumber=(POKER_NUMBER-currentIndex)/n;
while(number>0)
{
Stringcurrent=POKERS[currentIndex++];
Stringpk=current.split("\"+T)[0].trim();
intcol=Integer.parseInt(current.split("\"+T)[1].trim());
if(null==results[m][col])
{
results[m][col]=pk+"";
}
else
{
results[m][col]+=pk+"";
}
number--;
}
for(inti=0;i<results.length;i++)
{
for(intj=0;j<results[i].length;j++)
{
if(null==results[i][j])
{
results[i][j]="";
}
}
}
drawPoker(--n,results);
}
/**
*给牌排序
*@paramorialPokersString原始扑克牌
*@paramtempString[]每张花色的扑克牌数组
*/
privatestaticvoidsortPokers(finalStringorialPokers,String[]temp)
{
Arrays.sort(temp,newComparator<String>()
{
@Override
publicintcompare(Stringo1,Stringo2)
{
intindex1=orialPokers.indexOf(o1+T);
intindex2=orialPokers.indexOf(o2+T);
if(index1>index2)
{
return-1;
}
elseif(index1<index2)
{
return1;
}
else
{
return0;
}
}
});
}
/**
*洗牌
*/
(String[]POKERS)
{
Collections.shuffle(Arrays.asList(POKERS));
}
publicstaticvoidmain(String[]args)
{
System.out.print("输入人数:");
Scannerscanner=newScanner(System.in);
intn=scanner.nextInt();
scanner.close();
String[]pColors=POKER_COLOR.split("\"+T);
String[][]results=newString[n][pColors.length];
shufflePokers(POKERS);
drawPoker(n,results);
finalStringpoker=Arrays.toString(ORIAL_POKERS);
System.out.println(" 输出如下: ");
for(inti=0;i<results.length;i++)
{
System.out.println("第"+(i+1)+"个人:");
for(intj=0;j<results[i].length;j++)
{
String[]temp=results[i][j].trim().split("\s+");
sortPokers(poker,temp);
System.out.println(pColors[j]+":"+Arrays.toString(temp).replaceAll("[\,\[\]]",""));
}
System.out.println();
}
}
}
㈨ Java扑克牌问题
使用JAVA编写以下程序:
1、请定义一个名为Card的扑克牌类,该类有两个private访问权限的字符串变量face和suit;分别描述一张牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“红桃”、“梅花”、“方块”);定义Card类中的public访问权限的构造方法,为类中的变量赋值;定义protected访问权限的方法getFace(),得到扑克牌的牌面值;定义protected访问权限的方法getSuit(),得到扑克牌的花色;定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。
2、若应用程序的main方法中,定义字符串数组f和s;分别表示扑克牌的牌面值和花色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
(1)使用Card类的构造方法给deck数组的52张牌赋值,要求数组中先存放黑桃花色的A、2、3、……、K;然后是红桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方块花色的A、2、3、……、K。请写出实现上述功能的程序段。
(2)请编写模拟洗牌的完整程序,即把数组deck中的扑克牌随机打乱存放顺序。
原题如下:
引用内容
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
解题,完成程序解:
程序代码
package poker;
public class Card {
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex);
if (splitIndex * 2 > size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex); //[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex); //[52]、22、30、22
} else if (splitIndex * 2 < size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex);
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交换
//SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
String face = "";
String suit = "";
for (int i = 0; i < size; i++) {
face = deck[srcIndex + i].face;
suit = deck[srcIndex + i].suit;
deck[srcIndex + i].face = deck[dstIndex + i].face;
deck[srcIndex + i].suit = deck[dstIndex + i].suit;
deck[dstIndex + i].face = face;
deck[dstIndex + i].suit = suit;
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一问解答
Card[] deck = new Card[52];
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"};
String s[] = {"Spade", "Heart", "Club", "Diamond"};
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < f.length; j++) {
deck[i * 13 + j] = new Card(s[i], f[j]); //依次存入数组之中,每13张一回合
}
}
//第二问解答
int rnd = 0;
int numOfShuffle = 10;
for (int i = 0; i < numOfShuffle; i++) {
rnd = (int) Math.abs(Math.random() * 52); //Math.random()返回大于或等于 0.0 但小于 1.0 的伪随机 double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; i<deck.length; i++)
{
System.out.println(deck[i]);
} */
}
}
㈩ java里怎样打乱(洗牌)一个数组
//给你个思路:给要给随机值,该随机值在索引范围内,然后从索引值里面取元素,在该元素对应
//的位置,进行二次随机取其他元素,然后进行交换,就可以了!
//还有更简单的办法,java早就想到了这一点,在集合工具里面就提供该方法,其实他内部也是
//这样的思路,用随机值进行交换!
importjava.util.*;
publicclassArrayListTest{
publicstaticvoidmain(String[]args){
Integer[]arr=newInteger[10];
for(inti=0;i<arr.length;i++){
arr[i]=i+1;
}
System.out.println(" 原序:"+Arrays.toString(arr)+" ");//原数组!
List<Integer>list=Arrays.asList(arr);//借用集合帮忙排序!
for(Integera:list){
Collections.shuffle(list);//随机排序!
System.out.println("随机:"+Arrays.toString(arr));//排序后!
}
}
}
//数字或者数量,你可以随意修改,二维数组也可以照用!