當前位置:首頁 » 編程語言 » java洗牌

java洗牌

發布時間: 2022-07-25 12:18:10

㈠ 怎麼寫一個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));//排序後!
}
}
}
//數字或者數量,你可以隨意修改,二維數組也可以照用!

熱點內容
怎麼看出車輛配置是不是舊車 發布:2025-01-28 00:42:42 瀏覽:3
編譯時的程序在哪裡 發布:2025-01-28 00:42:39 瀏覽:345
ftp協議的功能 發布:2025-01-28 00:38:53 瀏覽:251
linux統計ip 發布:2025-01-28 00:38:50 瀏覽:153
游戲解壓泥 發布:2025-01-28 00:38:04 瀏覽:728
curlandroid下載 發布:2025-01-28 00:20:10 瀏覽:873
數控車編程視頻 發布:2025-01-28 00:15:02 瀏覽:967
壓縮機帶電 發布:2025-01-28 00:06:14 瀏覽:195
雲存儲拓撲圖 發布:2025-01-27 23:58:00 瀏覽:830
中央編譯局女博士微博 發布:2025-01-27 23:44:24 瀏覽:698