10_9_5为Ch10CardLib库编写一个控制台客户程序,从扰乱的Deck对象中一次取出5张牌.如果这5张牌都是相同的花色,客户程序就应在屏幕上显示这5张牌,以及文本"Flush",否则就显示50
2012-10-10 01:35
866 查看
//file name Suit.Cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { public enum Suit { Club, //梅花 Diamond, //方块 Heart, //红桃 Spade //黑桃. } } //file name Rank.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { public enum Rank //牌的点数. { Ace=1, Deuce, Three, Four, Five, Six, Seven, Eight, Nice, Ten, Jack, Queen, King } } //file name Cards.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { //Card 表示一张标准的扑克牌,包含4种花色,顺序是A-K public class Card { //修改为公开的字段. public readonly Suit suit; public readonly Rank rank; public Card() { } public Card(Suit newSuit, Rank newRank) { suit = newSuit; rank = newRank; } public override string ToString() { return "The " + rank + " of " + suit + "s"; } } } //file name Deck.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch10CardLib { public class Deck { private Card[] cards; public Deck() { cards=new Card[52]; for(int suitval=0;suitval<4;suitval++) { for(int rankVal=1;rankVal<14;rankVal++) { cards[suitval*13+rankVal-1]=new Card((Suit)suitval,(Rank)rankVal); } } } public Card GetCard(int cardNum) { if(cardNum>=0&&cardNum<=51) { return cards[cardNum]; } else { throw (new System.ArgumentOutOfRangeException("cardNum",cardNum, "Value must be between 0 and 51.")); } } public void Shuffle() { Card[] newDeck = new Card[52]; bool[] assigned = new bool[52]; Random sourceGen = new Random(); for (int i = 0; i < 52; i++) { int destCard = 0; bool foundcard = false; while (foundcard == false) { destCard = sourceGen.Next(52); if (assigned[destCard] == false) foundcard = true; } newDeck[destCard] = cards[i]; assigned[destCard] = true; } newDeck.CopyTo(cards, 0); } }//end deck. } // CopyTo摘要: // 将当前一维 System.Array 的所有元素复制到指定的一维 System.Array 中(从指定的目标 System.Array 索引开始)。索引指定为 // 32 位整数。 // // 参数: // array: // 一维 System.Array,它是从当前 System.Array 复制的元素的目标位置。 // // index: // 一个 32 位整数,它表示 array 中复制开始处的索引。 //file name Ch10CardClient.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ch10CardLib; namespace Ch10CardClient { class Program { static void Main(string[] args) { Deck myDeck = new Deck(); Card[] cards; int numSuit=0; int icount=0; cards = new Card[5]; // while (true) // { myDeck.Shuffle(); icount = 0; for (int i = 0; i < 5; i++) { cards[i] = myDeck.GetCard(i); } for (int i = 0; i < 5; i++) { if (cards[0].suit == cards[i].suit) { icount++; } } // if (icount == 5) // break; // } if (icount == 5) { Console.WriteLine("Flush .\n "); for (int i = 0; i < 5; i++) { Console.WriteLine( cards[i].ToString()); } } else { Console.WriteLine("No flush .\n"); for (int i = 0; i < 50; i++) { Console.WriteLine(myDeck.GetCard(i)); } } // Console.ReadKey(); } } }
Ps:可以修改程序,得出5张牌为同花顺要循环多少次.
相关文章推荐
- 编写了一个服务器端的程序实现在客户端输入字符然后在控制台上显示,直到输入"END"为止,让你写出客户端的程序;
- 编写一个显示“欢迎学习Java"的Java Applet程序
- 编写一个程序,从标准输入一行一行地读取文本,并完成如下任务:如果文件中有两行或更多行相邻的文本内容相同,那么就打印其中一行,其余行不打印。
- 编写一段程序,从标准输入读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止。使用while循环一次读取一个单词,当一个单词连续出现两次是使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有人任何单词是重复出现的。
- 用mingw编译C++工程;程序里面的cout<<"aaa";cmd运行c++程序,aaa内容在控制台不显示,如何将aaa输出到控制台屏幕并将控制台内容写到一个文件中。
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- 编写一个程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应的单词
- [OS] 用汇编语言编写一个Boot Sector显示"Hello world!" [李园7舍_404]
- 汇编语言: 编写一个程序,从键盘输入一个 0~65535 之间的 10 进制无符号数,然后以 16 进制 和四进制数形式显示出所输入的数。
- 写一个彩票程序 30选7 随机(1~30中间)生成7个随机数,注意不能重复然后输入7个数,对比7个数是否与随机数有相同的,如果有显示“中了几个号” 如果中了7个号,显示一等奖 如果中了6个号,显示二
- 通过编写一个程序,来说明字符数组 以及操作字符数组的函数的用法。该程序读入一组文本行,并把最长的文本行打印出来。
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- 10_9_2从类MyClass中派生一个类MyDervedClass,重写GetString(),使用该方法的基类实现代码从基类中返回一个字符串,但在返回的字符串中添加文本"(output fro)"
- 10、取出一个字符串中字母出现的次数。如:字符串:"abcde%^kka27qoq" ,输出格式为: a(2)b(1)k(2)...
- 输入一个整数,如果该整数x符合条件:-1<x<10,则输出“ok”,否则输出“not ok”
- 第四题(迅雷笔试题):编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推
- 《编写一个程序,从一个文件中读出字符串,并显示在屏幕上》
- 笔试题:编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。