D
Delphi_lobby
Unregistered / Unconfirmed
GUEST, unregistred user!
此问题为《C#入门经典》207 页的例子
工程Ch10CardLib,有两个类,可以快速浏览。这两个类没有错误:
//一
using System;
namespace Ch10CardLib
{
public enum Suit // 枚举 花色
{
Club, // 梅花
Diamond, // 方块
Heart, // 黑桃
Spade // 红桃
}
public enum Rank // 枚举 所有牌
{
Ace = 1,
Deuce,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
}
public class Card
{
public readonly Suit suit; //定义两个只读域
public readonly Rank rank; //定义两个只读域
public override string ToString() //重写ToString()
{
return "The "+rank+" of "+suit+" s";
}
private Card() //定义一个私有的构造函数
{
}
public Card(Suit newSuit, Rank newRank) //初始化只读域
{
suit = newSuit;
rank = newRank;
}
}
}
//二:
using System;
namespace Ch10CardLib
{
public class Deck
{
private Card[] cards;
//定义CARD的对象的私有数组
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 Shuffe() //
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
for (int i = 1;
i < 52;
i++)
{
int destCard = 0;
bool foundCard = false;
Random sourceGen = new Random();
while (foundCard == false)
{
destCard = sourceGen.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards;
}
cards = newDeck;
}
}
}
这两个类经过检查没有错误,错误得在下面:
新建一工程:ConsoleApplication16
包函类:Class1.cs 右键工程添加引用成功!代码如下:
using System;
using Ch10CardLib;
namespace Ch10CardClient
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Deck myDeck = new Deck();
myDeck.Shuffe();
for (int i = 0;
i < 52;
i ++)
{
Card tempCard = myDeck.GetCard(i);
Console.Write(tempCard.ToString());
if (i != 51)
Console.Write(",");
else
Console.WriteLine();
}
}
}
}
//奇怪报错提示为这句,而且是循环中间报错的,也就是说可以循环随机次数就报错!有的时候循环46次,有的时候循环33次才报错,循环次数不固定的!应该循环52次,程序才算终止!
报错提示:未处理的“System.NullReferenceException”类型的异常出现在ConsloeApplication16.exe中。其它信息:未将对象引用设置到对像的实例。
工程Ch10CardLib,有两个类,可以快速浏览。这两个类没有错误:
//一
using System;
namespace Ch10CardLib
{
public enum Suit // 枚举 花色
{
Club, // 梅花
Diamond, // 方块
Heart, // 黑桃
Spade // 红桃
}
public enum Rank // 枚举 所有牌
{
Ace = 1,
Deuce,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King
}
public class Card
{
public readonly Suit suit; //定义两个只读域
public readonly Rank rank; //定义两个只读域
public override string ToString() //重写ToString()
{
return "The "+rank+" of "+suit+" s";
}
private Card() //定义一个私有的构造函数
{
}
public Card(Suit newSuit, Rank newRank) //初始化只读域
{
suit = newSuit;
rank = newRank;
}
}
}
//二:
using System;
namespace Ch10CardLib
{
public class Deck
{
private Card[] cards;
//定义CARD的对象的私有数组
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 Shuffe() //
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
for (int i = 1;
i < 52;
i++)
{
int destCard = 0;
bool foundCard = false;
Random sourceGen = new Random();
while (foundCard == false)
{
destCard = sourceGen.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards;
}
cards = newDeck;
}
}
}
这两个类经过检查没有错误,错误得在下面:
新建一工程:ConsoleApplication16
包函类:Class1.cs 右键工程添加引用成功!代码如下:
using System;
using Ch10CardLib;
namespace Ch10CardClient
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Deck myDeck = new Deck();
myDeck.Shuffe();
for (int i = 0;
i < 52;
i ++)
{
Card tempCard = myDeck.GetCard(i);
Console.Write(tempCard.ToString());
if (i != 51)
Console.Write(",");
else
Console.WriteLine();
}
}
}
}
//奇怪报错提示为这句,而且是循环中间报错的,也就是说可以循环随机次数就报错!有的时候循环46次,有的时候循环33次才报错,循环次数不固定的!应该循环52次,程序才算终止!
报错提示:未处理的“System.NullReferenceException”类型的异常出现在ConsloeApplication16.exe中。其它信息:未将对象引用设置到对像的实例。