import java.util.ArrayList;
創(chuàng)新互聯(lián)是專業(yè)的工布江達(dá)網(wǎng)站建設(shè)公司,工布江達(dá)接單;提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行工布江達(dá)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
import java.util.Random;
public class PokerDemo {
ArrayListCard poker = new ArrayListCard();
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("發(fā)牌后: ");
for(int i = 0; i players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayListCard tmp = new ArrayListCard();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayListCard(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 {
ArrayListCard cards = new ArrayListCard();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
package xly04;
import java.util.Random;
public class ArrayDemo3 {
/**
* 數(shù)組應(yīng)用:
* 1 隨機(jī)洗牌
* 2 順序發(fā)牌
*/
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)];//長(zhǎng)度為8,隨機(jī)下標(biāo)[0,8)
//System.out.println(card);//隨機(jī)的某張牌
for(int i=cards.length-1;i=1;i--){
int j=new Random().nextInt(i);//或者(1)不注銷(xiāo)用int j=random.nextInt(i);
String t=cards[i];
cards[i]=cards[j];
cards[j]=t;
}
for(int i=0;icards.length;i++){
System.out.print(cards[i]+",");
}
System.out.println();//輸出回車(chē)字符
//順序發(fā)牌:順序的將每個(gè)牌發(fā)給每個(gè)人
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;icards.length;i++){
//i=0,1,2,3,4,5,6,7
String card=cards[i];//card 代表每張撲克牌
String player=players[i%players.length];
//player代表每個(gè)玩游戲的人
System.out.println(player+":"+card);
}
}
}
你指的是Card里的toString方法吧
public?String?toString()?{
String?aa?=?suit?+?"?"?+?num?+?"??";
return?aa;
}
toString這個(gè)方法一般在?System.out.print時(shí)使用,這個(gè)是打印出String,JVM就是默認(rèn)調(diào)用類(lèi)的toSting方法
注:所有類(lèi)都有toString方法,默認(rèn)是當(dāng)前對(duì)象的hashcode,即內(nèi)存地址
所以在發(fā)牌是打印
public?void?dealcard()//發(fā)牌
{
for(int?i=0;i52;i++)
{
if(i%4==0i!=0){
System.out.println();??//每發(fā)4張牌輸出換行
}
//?就是這里,默認(rèn)調(diào)用card的toString方法
System.out.print(card[i]);?//依次輸出?發(fā)的牌
}
}