-1

我编写了以下类来返回一个随机数,例如掷骰子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameTest
{
    class Dice
    {
    public int publicMinNum
    {
        get { return _minNum; }
        set { _minNum = value; }
    }

    public int publicMaxNum
    {
        get { return _maxNum; }
        set { _maxNum = value; }
    }

    static int _minNum;
    static int _maxNum;

    static Random diceRoll = new Random();
    public int rolled = diceRoll.Next(_minNum, _maxNum);
 }
}

这个类在我的表单中被调用了几次:

    private void btnPushMe_Click(object sender, EventArgs e)
    {
        Dice myRoll = new Dice();
        myRoll.publicMinNum = 1;
        myRoll.publicMaxNum = 7;

        lblMain.Text = myRoll.rolled.ToString();

        Dice mySecondRoll = new Dice();
        mySecondRoll.publicMinNum = 1;
        mySecondRoll.publicMaxNum = 13;

        lblMain2.Text = mySecondRoll.rolled.ToString();
    }

如您所见,我将课程称为myRoll和两次mySecondRoll。我认为这样做会创建类的单独实例并输出两个单独的数字(一个在 1 和 6 之间,另一个在 1 和 12 之间)

我遇到的问题是:

1)第一个数字总是0。

2)类的两个实例相互干扰,即。应该在 1 到 6 之间的数字不是。

我想知道,不仅仅是如何修复代码,还想解释一下这里发生了什么以及为什么,谢谢。

4

6 回答 6

5

问题是您将 Dice 类中的字段声明为static. 这意味着该变量将只有一个实例,它将在应用程序内类的所有实例之间共享。

以下行:

public int rolled = diceRoll.Next(_minNum, _maxNum);

...在您new Dice()创建. 你可以把它变成一个属性,所以代码会等待运行,直到你要求它:_minNum_maxNum0

public int Rolled { get { return diceRoll.Next(_minNum, _maxNum); } }

...但通常不会仅通过询问属性值来更改属性。这种代码往往会产生所谓的Heisenbugs,它们很难追踪,因为系统的行为只是通过尝试观察而改变。

因此,您可以通过以下一种方式重新编写您的类,使用一种Roll()方法来实际执行滚动,以及一个允许代码在必要时继续检查最后滚动值的属性:

public class Die
{

    // Using a constructor makes it obvious that you expect this
    // class to be initialized with both minimum and maximum values.
    public Die(int minNum, int maxNum)
    {
        // You may want to add error-checking here, to throw an exception
        // in the event that minNum and maxNum values are incorrect.

        // Initialize the values.
        MinNum = minNum;
        MaxNum = maxNum;

        // Dice never start out with "no" value, right?
        Roll();
    }

    // These will presumably only be set by the constructor, but people can
    // check to see what the min and max are at any time.
    public int MinNum { get; private set; }

    public int MaxNum { get; private set; }

    // Keeps track of the most recent roll value.
    private int _lastRoll;

    // Creates a new _lastRoll value, and returns it.
    public int Roll() { 
        _lastRoll = diceRoll.Next(MinNum, MaxNum);
        return _lastRoll;
    }

    // Returns the result of the last roll, without rolling again.
    public int LastRoll {get {return _lastRoll;}}

    // This Random object will be reused by all instances, which helps
    // make results of multiple dice somewhat less random.
    private static readonly Random diceRoll = new Random();
}

(注意“die”是“dice”的单数形式)。用法:

private void btnPushMe_Click(object sender, EventArgs e)
{
    Die myRoll = new Die(1, 7);
    lblMain.Text = myRoll.Roll().ToString();

    Die myRoll2 = new Die(1, 13);
    lblMain2.Text = mySecondRoll.Roll().ToString();
}
于 2013-06-11T21:11:50.260 回答
5

问题二已经回答了:因为变量是静态的:

static int _minNum;
static int _maxNum;

另一方面,问题一还没有回答,所以这里是:

public int rolled = diceRoll.Next(_minNum, _maxNum);

这不是一些动态调用。这是一个字段初始化,甚至会在构造函数之前设置。您可以通过第一次通过骰子进行调试来检查这一点。

此时两者_minNum_maxNum仍为 0,因此rolled 将设置为 0

这也可以通过将rolled变成一个属性来解决:

    public int rolled
    {
        get { return diceRoll.Next(_minNum, _maxNum); }
    }

目前_minNum_maxNum第一次设置是因为它们是静态的,因此当你创建第二个骰子时,它们已经设置好了。

编辑,因为有人提出建议,这就是我创建它的方式:

骰子

class Dice
{
    private static Random diceRoll = new Random();

    private int _min;
    private int _max;
    public int Rolled { get; private set; }

    public Dice(int min, int max)
    {
        _min = min;
        _max = max;

        // initializes the dice
        Rolled = diceRoll.Next(_min, _max);
    }

    public int ReRoll
    {
        get
        {
            Rolled = diceRoll.Next(_min, _max);
            return Rolled;
        }
    }
}

请注意,骰子有两个属性:RolledReRoll。因为您的意图不清楚,所以我添加了两者来说明行为。

Rolled由构造函数设置。如果你想要一个新号码,你可以ReRoll

如果您确实故意希望掷骰子的生命周期为每个骰子一个(但我不这么认为),您将删除该ReRoll方法。

骰子的名称如下:

    private static void Main(string[] args)
    {
        Dice myRoll = new Dice(1, 7);

        // All the same
        var result1 = myRoll.Rolled.ToString();
        var result2 = myRoll.Rolled.ToString();
        var result3 = myRoll.Rolled.ToString();

        // something new
        var result4 = myRoll.ReRoll.ToString();

        Dice mySecondRoll = new Dice(1, 13);
        var result = mySecondRoll.ReRoll.ToString();
    }
于 2013-06-11T21:23:37.760 回答
1

我会将您的课程更改为如下所示:

class Dice
{
  // These are non-static fields. They are unique to each implementation of the
  // class. (i.e. Each time you create a 'Dice', these will be "created" as well.
  private int _minNum, _maxNum;

  // Readonly means that we can't set _diceRand anywhere but the constructor.
  // This way, we don't accidently mess with it later in the code.
  // Per comment's suggestion, leave this as static... that way only one
  // implementation is used and you get more random results.  This means that
  // each implementation of the Dice will use the same _diceRand
  private static readonly Random _diceRand = new Random();

  // A constructor allows you to set the intial values.
  // You would do this to FORCE the code to set it, instead
  // of relying on the programmer to remember to set the values
  // later.
  public Dice(int min, int max)
  {
    _minNum = min;
    _maxNum = max;
  }

  // Properties
  public Int32 MinNum
  {
    get { return _minNum; }
    set { _minNum = value; }
  }

  public Int32 MaxNum
  {
    get { return _maxNum; }
    set { _maxNum = value; }
  }

  // Methods
  // I would make your 'rolled' look more like a method instead of a public
  // a variable.  If you have it as a variable, then each time you call it, you
  // do NOT get the next random value.  It only initializes to that... so it would
  // never change.  Using a method will have it assign a new value each time.
  public int NextRoll()
  {
    return _diceRand.Next(_minNum, _maxNum);
  }    
}
于 2013-06-11T21:12:20.233 回答
1

您的 get/setter 支持字段被标记为“ static”。如果变量声明为“ static”,则该值将在整个应用程序中持久保存,并在它们所在类型的不同实例之间共享。

这里

还,

由于您的类属性不包含任何逻辑,我建议使用“ automatic”属性。

    class Dice
    {
      public int publicMinNum { get; set; }
      public int publicMaxNum { get; set; }
      Random diceRoll = new Random();
      public int rolled = diceRoll.Next(publicMinNum , publicMaxNum );
    }

教程在automatic properties 这里

于 2013-06-11T21:12:36.720 回答
1

你的问题是由于static成员。

从关于static的 MSDN 文档中,“虽然类的实例包含该类的所有实例字段的单独副本,但每个静态字段只有一个副本。”

于 2013-06-11T21:12:58.847 回答
1

我认为这里真正的问题是您还没有完全正确地建模 Die。

骰子有一个最小值和最大值(定义范围的开始和结束),但是一旦制作了骰子,您就无法更改此值,即六面骰子不会变成八面骰子。因此,不需要公共二传手。

现在,并非所有 die 共享相同的范围,这是特定于每个 die 的东西,因此这些属性应该属于实例而不是static

同样,每个骰子对象都有一个CurrentRoll代表正面朝上数字的值,这确实是随机生成的。然而,要改变CurrentRoll你需要的模具Roll

这使得 Die 的实现看起来像

class Die
{
    private static Random _random;

    public int CurrentRoll { get; private set; }

    public int Min { get; private set; }

    public int Max { get; private set; }

    public Die(int min, int max)
    {
        Min = min;
        Max = max;
        Roll();
    }

    public int Roll()
    {
        CurrentRoll = _random.Next(Min, Max+1); // note the upperbound is exlusive hence +1
        return CurrentRoll;
    }
}

你会像这样使用它

public static void Main()
{
    Die d1 = new Die(1, 6);
    Die d2 = new Die(1, 6);

    Console.WriteLine(d1.Roll());
    Console.WriteLine(d2.Roll());
    //...
}

演示

于 2013-06-11T22:20:32.743 回答