| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:编写小程序半天也弄不出来
只看楼主 加入收藏
zhongnan312
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-11-4
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
编写小程序半天也弄不出来
请高手帮忙谢谢!
程序是这样的:开始——初始化54张牌——少于100次洗牌(随机交换俩张牌的位置)——分发54张牌——发牌(发3个人的牌)
——打印出自己的牌——结束
搜索更多相关主题的帖子: 编写 
2009-11-04 20:45
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:10 
import java.util.*;

class Card {
    private String face;
    private String suit;
   
    Card(String face, String suit) {
        this.face = face;
        this.suit = suit;
    }
   
    public String toString() {
        return suit+face;
    }
   
}

class Person {
    ArrayList<Card> cards;
   
    Person() {
        cards = new ArrayList<Card>();
    }
   
    public void getCard(Card card) {
        cards.add(card);
    }
   
    public void showCards() {
        for (int i=0; i<cards.size(); i++) {
            System.out.print(cards.get(i)+" ");
        }
        System.out.println();
    }
}

class CardGame {
    LinkedList<Card> deck;
   
    CardGame() {
        init();
    }
   
    public void init() {
        String faces[] = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String suits[] = {"黑桃","红桃","梅花","方块"};
        
        deck = new LinkedList<Card>();
        
        for (int i=0; i<52; i++) {
            deck.add(new Card(faces[i%13], suits[i/13]));
        }
        
        deck.add(new Card("鬼","红"));
        deck.add(new Card("鬼","黑"));
    }
   
    public void shuffle() {
        for( int i = 0; i < deck.size(); i++ ){
        int j = ( int )( Math.random() * 54 );
        Card temp = deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,temp);
      }
    }
}
public class TestCardGame {
        public static void main(String[] args) {
        CardGame cg = new CardGame();
        for (int i=0; i<100; i++)
            cg.shuffle();//洗牌100次
        Person a = new Person();
        Person b = new Person();
        Person c = new Person();
            
        while(cg.deck.size()!=0) {//分牌给三个人
            a.getCard(cg.deck.pop());
            b.getCard(cg.deck.pop());
            c.getCard(cg.deck.pop());
        }
        
        a.showCards();//打印手中的牌
        b.showCards();
        c.showCards();
        
    }
}

[ 本帖最后由 lampeter123 于 2009-11-5 11:20 编辑 ]

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-11-05 10:18
vecomwa
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:63
专家分:138
注 册:2009-9-1
收藏
得分:0 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ZhiPai {
    public static void main(String[] args) {
        int x,y;
        Card card1 = null;
        Card card2 = null;

        //先定义两个随机数,待会洗牌的时候会用到
        Random x1 = new Random();
        Random x2 = new Random();
        
        //使用泛型,定义了四个容器,分别用来存放整幅牌玩家1、玩家2和自己的牌
        List<Card> cards = new ArrayList<Card>();
        List<Card> wanjia1 = new ArrayList<Card>();
        List<Card> wanjia2 = new ArrayList<Card>();
        List<Card> my = new ArrayList<Card>();
        
        //添加一对王牌
        cards.add(new Card(0,"红鬼"));
        cards.add(new Card(0,"黑鬼"));
        
        //初始化一副牌
        for(int i=1; i<=13; i++) {
            cards.add(new Card(i,"黑桃"));
            cards.add(new Card(i,"红桃"));
            cards.add(new Card(i,"梅花"));
            cards.add(new Card(i,"方块"));
        }
        
        //洗牌
        for(int i=0; i<100; i++) {
            x = x1.nextInt(cards.size());
            y = x2.nextInt(cards.size());
            
            card1 = cards.get(x);
            card2 = cards.get(y);
            cards.set(x, card2);
            cards.set(y, card1);
        }
        
        //发牌
        for(int i=0; i<cards.size();) {
            my.add(cards.get(i++));
            wanjia1.add(cards.get(i++));
            wanjia2.add(cards.get(i++));
        }
        //打印
        for(int i=0,j=0; i<my.size(); i++) {
            x = my.get(i).id;
            switch(x){
            case 11:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            case 12:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            case 13:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            }
            System.out.print(my.get(i).s + " " + my.get(i).id + "   ");
            j++;
            if(j%6==0) System.out.println();
        }
    }
   
}

//将牌定义为一个单独的内。原本发牌洗牌这些都应该在这个内里面的,这样更符合面向对象的思想。不过为了个人写起来更简便,所以直接把它们全部写到main()里面去了
class Card {
    int id;
    String s;
   
    Card(int id, String s) {
        this.id = id;
        this.s = s;
    }
}

[ 本帖最后由 vecomwa 于 2009-11-5 10:57 编辑 ]
2009-11-05 10:45
zhongnan312
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-11-4
收藏
得分:0 
以下是引用lampeter123在2009-11-5 10:18:46的发言:

import java.util.*;

class Card {
    private String face;
    private String suit;
   
    Card(String face, String suit) {
        this.face = face;
        this.suit = suit;
    }
   
    public  ...
谢谢你啊,对于你来说这个应该很简单,请谅解初学者打扰你的意思,愿你一切顺利
2009-11-05 20:32
快速回复:编写小程序半天也弄不出来
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027523 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved