用C#模拟实现扑克牌发牌、随机排序程序。 报错

34818060 2016-06-18 07:48:55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Test5;
//用C#模拟实现扑克牌发牌、随机排序程序。


//(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。


//(2)最后将四个人的扑克牌包括花色打印在控制台上。


//其中:


// 花色和点数用枚举类型实现


// 每张扑克牌用类实现


//提示:可以用ArrayList初始化52张扑克牌,然后从这个动态数组中随机取牌发给四个玩家,直到它为空为止。

namespace Test5
{
enum Color { HongTao = -1, HeiTao = -2, MeiHua = -3, FangPian = -4 }//花色
enum point { A, two, three, four, five, six, seven, eight, nine, ten, J, Q, K }//点数
class Poker //定义poker类
{ //扑克
private string p1, p2;
public Poker(string x, point y)//构造函数
{

this.p1 = x;

switch (y)
{
case point.two: this.p2 = "2"; break;
case point.three :this.p2="3";break;
case point.four:this .p2="4";break;
case point.five:this .p2="4";break;
case point.six:this.p2="4";break;
case point.seven:this.p2="4";break;
case point.eight:this.p2="4";break;
case point.nine:this .p2="4";break;
case point.ten: this.p2 = "4"; break;

}

}
public void Printp()//方法成员
{
Console.Write("({0},{1})", this.p1, this.p2);
}
}
class program
{

static void Main(string[] args)
{
ArrayList myPoker = new ArrayList();//实例化一个ArrayList存放所有的扑克牌
ArrayList Person1 = new ArrayList();
ArrayList Person2 = new ArrayList();
ArrayList Person3 = new ArrayList();
ArrayList Person4 = new ArrayList();
Random r = new Random();
for (int i = -4; i <= -1; i++) //外循环初始化扑克的花色
{
for (int j = 0; j <= 12; j++) //内层循环初始化扑克的点数
{
myPoker.Add(new Poker
(
Enum.GetName(typeof(Color), i),
Enum.GetName(typeof(point), j) //往mypoker内添加已设计好的枚举元素
));

}
} //for循环结束,52张扑克牌已经生成
Console.WriteLine("打印所有的扑克牌:");
for (int i = 0; i < 52; i++) //打印52张生成的扑克牌
{
Poker poAll = (Poker)myPoker[i];//访问mypoker中的元素
poAll.Printp();//调用Printp方法
}

//开始发牌,一个人一个人的发,每发一张牌得从myPoker中RemoveAt掉扑克牌,Count数减小;第一个人Add到一张牌。
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);//分配随机数
Person1.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person2.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person3.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person4.Add(myPoker[te]);
myPoker.RemoveAt(te);
}

//打印四个人的扑克牌
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("打印第一个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po1 = (Poker)Person1[i];//访问person1中的元素
po1.Printp();//再次调用Printp方法
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("打印第二个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po2 = (Poker)Person2[i];
po2.Printp();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("打印第三个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po3 = (Poker)Person3[i];
po3.Printp();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("打印第四个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po4 = (Poker)Person4[i];
po4.Printp();
}
Console.WriteLine();
Console.WriteLine();
}
}
}

错误 2 参数 2: 无法从“string”转换为“Test5.point” D:\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs 77 25 ConsoleApplication2

错误 1 与“Test5.Poker.Poker(string, Test5.point)”最匹配的重载方法具有一些无效参数 D:\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs 74 33 ConsoleApplication2



这是什么问题怎么解决,我只是想用枚举类把2到10的数字以数字形式输出,求助?







...全文
544 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yayaymm 2017-05-26
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using Test5; namespace Test5 { enum Color { HongTao = -1, HeiTao = -2, MeiHua = -3, FangPian = -4 } enum point { A, two, three, four, five, six, seven, eight, nine, ten, J, Q, K } class Poker { private string p1, p2; public Poker(string x, point y) { this.p1 = x; switch (y) { case point.two: this.p2 = "2"; break; case point.three :this.p2="3";break; case point.four:this .p2="4";break; case point.five:this .p2="4";break; case point.six:this.p2="4";break; case point.seven:this.p2="4";break; case point.eight:this.p2="4";break; case point.nine:this .p2="4";break; case point.ten: this.p2 = "4"; break; } } public void Printp() { Console.Write("({0},{1})", this.p1, this.p2); } } class program { static private void Main(string[] args) { ArrayList myPoker = new ArrayList(); ArrayList Person1 = new ArrayList(); ArrayList Person2 = new ArrayList(); ArrayList Person3 = new ArrayList(); ArrayList Person4 = new ArrayList(); Random r = new Random(); for (int i = -4; i <= -1; i++) { for (int j = 0; j <= 12; j++) { myPoker.Add=new.Poker ( ( Enum.GetName(typeof(Color), i)), Enum.GetName(typeof(point), j)); } } Console.WriteLine("打印所有的扑克牌:"); for (int i = 0; i < 52; i++) { Poker poAll = (Poker)myPoker[i]; poAll.Printp(); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person1.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person2.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person3.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person4.Add(myPoker[te]); myPoker.RemoveAt(te); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第一个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po1 = (Poker)Person1[i]; po1.Printp(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第二个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po2 = (Poker)Person2[i]; po2.Printp(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第三个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po3 = (Poker)Person3[i]; po3.Printp(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第四个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po4 = (Poker)Person4[i]; po4.Printp(); } Console.WriteLine(); Console.WriteLine(); } } } //我想知道哪里错了
34818060 2016-06-20
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
1、Enum.GetName(typeof(point), j) 得到的是字符串 所以会有 与“Test5.Poker.Poker(string, Test5.point)”最匹配的重载方法具有一些无效参数 public Poker(string x, point y)//构造函数 应改为 public Poker(string x, string y)//构造函数 2、既然 y 已经是 string 的了 那么就要按字符串进行比较
            switch (y)
            {
                case "two": this.p2 = "2"; break;
                case "three": this.p2 = "3"; break;
                case "four": this.p2 = "4"; break;
                case "five": this.p2 = "4"; break;
                case "six": this.p2 = "4"; break;
                case "seven": this.p2 = "4"; break;
                case "eight": this.p2 = "4"; break;
                case "nine": this.p2 = "4"; break;
                case "ten": this.p2 = "4"; break;
                default: p2 = y; break;
            }
加了句 default: p2 = y; break; 否在对于 A、J、Q、K 都显示为空了 一个可能的结果
感谢,终于解决了~
34818060 2016-06-20
  • 打赏
  • 举报
回复
引用 6 楼 wanghui0380 的回复:
额,这是作业把,怎么都一个问题 看这里把 http://bbs.csdn.net/topics/391966730 当然你这是作业,俺们滴回复你可以略过,只告诉你那位老兄始终没明白滴东西 (int)enum.xxx 可以转成int,同样 (xx枚举)int也可以转成xx枚举
就是作业啊,没得办法
xuzuning 2016-06-20
  • 打赏
  • 举报
回复
其实你的两个枚举,意义实在不大 真不如改成两个字符串数组
xuzuning 2016-06-20
  • 打赏
  • 举报
回复
1、Enum.GetName(typeof(point), j) 得到的是字符串
所以会有 与“Test5.Poker.Poker(string, Test5.point)”最匹配的重载方法具有一些无效参数
public Poker(string x, point y)//构造函数
应改为
public Poker(string x, string y)//构造函数

2、既然 y 已经是 string 的了
那么就要按字符串进行比较
            switch (y)
{
case "two": this.p2 = "2"; break;
case "three": this.p2 = "3"; break;
case "four": this.p2 = "4"; break;
case "five": this.p2 = "4"; break;
case "six": this.p2 = "4"; break;
case "seven": this.p2 = "4"; break;
case "eight": this.p2 = "4"; break;
case "nine": this.p2 = "4"; break;
case "ten": this.p2 = "4"; break;
default: p2 = y; break;
}
加了句 default: p2 = y; break; 否在对于 A、J、Q、K 都显示为空了
一个可能的结果

wanghui0380 2016-06-20
  • 打赏
  • 举报
回复
你试试看 (int)point.two是什么,应该是1把,因为索引是0开始,自然two是1,那么你加个1就是
wanghui0380 2016-06-20
  • 打赏
  • 举报
回复
额,这是作业把,怎么都一个问题 看这里把 http://bbs.csdn.net/topics/391966730 当然你这是作业,俺们滴回复你可以略过,只告诉你那位老兄始终没明白滴东西 (int)enum.xxx 可以转成int,同样 (xx枚举)int也可以转成xx枚举
34818060 2016-06-20
  • 打赏
  • 举报
回复
引用 4 楼 caozhy 的回复:
new Poker需要的参数是xy,你传的不知道是什么 Enum.GetName(typeof(point), j) 这里返回的是string
那请问具体可以怎么改
Poopaye 2016-06-19
  • 打赏
  • 举报
回复
你自己写的构造函数却不会调用!!!
threenewbee 2016-06-19
  • 打赏
  • 举报
回复
new Poker需要的参数是xy,你传的不知道是什么 Enum.GetName(typeof(point), j) 这里返回的是string
34818060 2016-06-19
  • 打赏
  • 举报
回复
引用 2 楼 shingoscar 的回复:
你自己写的构造函数却不会调用!!!
我是新手,有点不懂,不晓得如何把 ArrayList和构造函数结合起来输出整数
34818060 2016-06-18
  • 打赏
  • 举报
回复
引用 楼主 qq_34818060 的回复:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; using Test5; //用C#模拟实现扑克牌发牌、随机排序程序。 //(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。 //(2)最后将四个人的扑克牌包括花色打印在控制台上。 //其中: // 花色和点数用枚举类型实现 // 每张扑克牌用类实现 //提示:可以用ArrayList初始化52张扑克牌,然后从这个动态数组中随机取牌发给四个玩家,直到它为空为止。 namespace Test5 { enum Color { HongTao = -1, HeiTao = -2, MeiHua = -3, FangPian = -4 }//花色 enum point { A, two, three, four, five, six, seven, eight, nine, ten, J, Q, K }//点数 class Poker //定义poker类 { //扑克 private string p1, p2; public Poker(string x, point y)//构造函数 { this.p1 = x; switch (y) { case point.two: this.p2 = "2"; break; case point.three :this.p2="3";break; case point.four:this .p2="4";break; case point.five:this .p2="5";break; case point.six:this.p2="6";break; case point.seven:this.p2="7";break; case point.eight:this.p2="8";break; case point.nine:this .p2="9";break; case point.ten: this.p2 = "10"; break; } } public void Printp()//方法成员 { Console.Write("({0},{1})", this.p1, this.p2); } } class program { static void Main(string[] args) { ArrayList myPoker = new ArrayList();//实例化一个ArrayList存放所有的扑克牌 ArrayList Person1 = new ArrayList(); ArrayList Person2 = new ArrayList(); ArrayList Person3 = new ArrayList(); ArrayList Person4 = new ArrayList(); Random r = new Random(); for (int i = -4; i <= -1; i++) //外循环初始化扑克的花色 { for (int j = 0; j <= 12; j++) //内层循环初始化扑克的点数 { myPoker.Add(new Poker ( Enum.GetName(typeof(Color), i), Enum.GetName(typeof(point), j) //往mypoker内添加已设计好的枚举元素 )); } } //for循环结束,52张扑克牌已经生成 Console.WriteLine("打印所有的扑克牌:"); for (int i = 0; i < 52; i++) //打印52张生成的扑克牌 { Poker poAll = (Poker)myPoker[i];//访问mypoker中的元素 poAll.Printp();//调用Printp方法 } //开始发牌,一个人一个人的发,每发一张牌得从myPoker中RemoveAt掉扑克牌,Count数减小;第一个人Add到一张牌。 for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count);//分配随机数 Person1.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person2.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person3.Add(myPoker[te]); myPoker.RemoveAt(te); } for (int i = 0; i < 13; i++) { int te = r.Next(0, myPoker.Count); Person4.Add(myPoker[te]); myPoker.RemoveAt(te); } //打印四个人的扑克牌 Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第一个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po1 = (Poker)Person1[i];//访问person1中的元素 po1.Printp();//再次调用Printp方法 } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第二个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po2 = (Poker)Person2[i]; po2.Printp(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第三个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po3 = (Poker)Person3[i]; po3.Printp(); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("打印第四个人的扑克牌:"); for (int i = 0; i < 13; i++) { Poker po4 = (Poker)Person4[i]; po4.Printp(); } Console.WriteLine(); Console.WriteLine(); } } } //////[size=24px] 上面有一个部分写错了,改一下 { case point.two: this.p2 = "2"; break; case point.three :this.p2="3";break; case point.four:this .p2="4";break; case point.five:this .p2="5";break; case point.six:this.p2="6";break; case point.seven:this.p2="7";break; case point.eight:this.p2="8";break; case point.nine:this .p2="9";break; case point.ten: this.p2 = "10"; break; }[/size] }[/size]

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧