这段小程序有错误 请指教

weikeli19 2017-01-19 07:30:08
这段程序有错误(你们上机试一下就知道了) 谁能帮我改一改 最好改动的幅度不要太大 保持原汁原味 谢谢了

代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace delete3
{
delegate void liweike(int x);

class Program
{
static void Main(string[] args)
{
A a = new A();
a.weikeni();

}
}
class A
{

public event liweike C;
public void weikeni()
{
int x = 4;

if (true)
{ C(x); }
}

}
class B
{
public A a ;
public A b ;
public B()
{
a = new A();
b = new A();
a.C += weikeli;
b.C += weikeli;
}
public void weikeli(int x)
{
Console.WriteLine(2);
}
}
}
...全文
613 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
weikeli19 2017-01-20
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
我说class B 哪里用了?
证明如下: Deck类里的方法改成如下 public Card GetCard(int cardNum) { if (cardNum >= 0 && cardNum <= 51) { if ((cardNum == 51) && (LastCardDrawn != null)) {Console.WriteLine("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO"); LastCardDrawn(this, EventArgs.Empty); } return cards[cardNum]; } else throw new CardOutOfRangeException(cards.Clone() as Cards); } ------------------------------------------------------------------------ Main()函数一开始添加如下代码: Deck a=new Deck(); a.GetCard(51); 若有OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO 则说明那个事件!=null 若没有OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO则说明那个事件==null
weikeli19 2017-01-20
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
我说class B 哪里用了?
这个 我懂了 不过这个程序还是有问题 他永远也得不到最后一张牌 他得倒的是洗过牌后的最后一张牌 并不是原先的牌的最后一张牌 你说呢?
Poopaye 2017-01-20
  • 打赏
  • 举报
回复
对的 可能是个bug,但也可能这个游戏的规则就是这样的。
weikeli19 2017-01-20
  • 打赏
  • 举报
回复
引用 13 楼 shingoscar 的回复:
后面那个例子别管用没用到,他调用事件前判断过null了,而你的例子里没判断过,区别就是这
不是 我不谈这个话题了 这个我已经懂了 现在问题是Deck类里的GetCard方法 不过这个程序还是有问题 他永远也得不到最后一张牌 他得倒的是洗过牌后的最后一张牌 并不是原先的牌的最后一张牌 你说呢?
Poopaye 2017-01-20
  • 打赏
  • 举报
回复
你8楼说你也有+=,这不重要,而是调用事件前应该判断事件是否为null
Poopaye 2017-01-20
  • 打赏
  • 举报
回复
后面那个例子别管用没用到,他调用事件前判断过null了,而你的例子里没判断过,区别就是这
weikeli19 2017-01-19
  • 打赏
  • 举报
回复
引用 9 楼 shingoscar 的回复:
我说class B 哪里用了?
书上的程序 对应的代码 和class B 等价的代码 也没用到啊 你举出来 我证明出来了 书上确实有问题
Poopaye 2017-01-19
  • 打赏
  • 举报
回复
我说class B 哪里用了?
weikeli19 2017-01-19
  • 打赏
  • 举报
回复
引用 7 楼 shingoscar 的回复:
你写了个classB,又没地方用到,当然有问题
我有加也用了呀 你看a.C += weikeli; b.C += weikeli; 还有这里 public void weikeni() { int x = 4; if (true) { C(x); } } 主程序里也调用了
Poopaye 2017-01-19
  • 打赏
  • 举报
回复
你写了个classB,又没地方用到,当然有问题
Poopaye 2017-01-19
  • 打赏
  • 举报
回复
public Game() { currentCard = 0; playDeck = new Deck(true); playDeck.LastCardDrawn += Reshuffle; playDeck.Shuffle(); discardedCards = new Cards(); } 这里+=过了,LastCardDrawn 就不是null了呀
weikeli19 2017-01-19
  • 打赏
  • 举报
回复
里面有完整的 控制台下的纸牌游戏
weikeli19 2017-01-19
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
if (true) ----> if (C != null)
给你们个网站 http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Visual-C-2012-Programming.productCd-1118314417,descCd-DOWNLOAD.html 下载Chapter 13 Code 下载解压后 点击Ch13CardLib
weikeli19 2017-01-19
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
if (true) ----> if (C != null)
那这两个类也应该有问题 我截取片段 你们看的懂 我标红的地方是不是永远不会执行事件 第一个类: public class Deck : ICloneable { public event EventHandler LastCardDrawn; private Cards cards = new Cards(); public Card GetCard(int cardNum) { if (cardNum >= 0 && cardNum <= 51) { if ((cardNum == 51) && (LastCardDrawn != null)) LastCardDrawn(this, EventArgs.Empty); return cards[cardNum]; } else throw new CardOutOfRangeException(cards.Clone() as Cards); } ............ 第2个类: public class Game { private int currentCard; private Deck playDeck; private Player[] players; private Cards discardedCards; public Game() { currentCard = 0; playDeck = new Deck(true); playDeck.LastCardDrawn += Reshuffle; playDeck.Shuffle(); discardedCards = new Cards(); } private void Reshuffle(object source, EventArgs args) { Console.WriteLine("Discarded cards reshuffled into deck."); ((Deck)source).Shuffle(); discardedCards.Clear(); currentCard = 0; }
巴士上的邂逅 2017-01-19
  • 打赏
  • 举报
回复
C没被注册,这种情况要这样:
if(C!=null)
    C(x);
Poopaye 2017-01-19
  • 打赏
  • 举报
回复
if (true) ----> if (C != null)

110,539

社区成员

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

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

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