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

.Net技术 码拜 8年前 (2016-02-28) 1636次浏览
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的数字以数字形式输出,求帮助?

解决方案

2

new Poker需要的参数是xy,你传的不知道是什么
Enum.GetName(typeof(point), j)
这里返回的是string

2

额,这是作业把,怎么都一个问题
看这里把
http://bbs.csdn.net/topics/391966730
当然你这是作业,俺们滴回复你可以略过,只告诉你那位老兄始终没明白滴东西  (int)enum.xxx 可以转成int,同样 (xx枚举)int也可以转成xx枚举

2

你试试看 (int)point.two是什么,应该是1把,原因是索引是0开始,自然two是1,那么你加个1就是

10

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 都显示为空了
一个可能的结果
用C#模拟实现扑克牌发牌、随机排序程序。 报错

4

其实你的两个枚举,意义实在不大
真不如改成两个字符串数组

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用C#模拟实现扑克牌发牌、随机排序程序。 报错
喜欢 (0)
[1034331897@qq.com]
分享 (0)