如何计算一个方法被使用的次数?

huangapple go评论151阅读模式
英文:

How do I count the number of times a method is used?

问题

"The value of dice 1 is: (生成的数字)"
"The value of dice 2 is: (生成的数字)"

英文:

如何计算一个方法被使用的次数?如何计算一个方法被使用的次数?I have 2 dice that generate a number between 1 and 6. I want to display their values as follows:
"The value of dice 1 is: (Generated number)"
"The value of dice 2 is: (Generated number)"

My counter doesn't seem to work because my output is as following:
"The value of dice 1 is: (Generated number)"
"The value of dice 1 is: (Generated number)"

    static void Main(string[] args)
    {
        Program DungeonDiceMasters = new Program();
        DungeonDiceMasters.Start();
    }

    void Start()
    {
        Dice d1, d2;
        d1 = new Dice();
        d2 = new Dice();

        d1.Throw();
        d2.Throw();

        d1.ShowValue();
        d2.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }


class Dice
{
    static Random NumberOneToSix = new Random();
    public int value;
    public int counter = 0;

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
    }

    public void ShowValue()
    {
        counter = counter + 1;
        Console.WriteLine("The value of dice " + counter + " is: " + value);
    }
}

答案1

得分: 1

每个 Dice 类的实例都有一个 counter 字段,因为该字段不是静态的。它们都具有值 1,因为每个骰子实例的 ShowValue 方法都已调用一次。

您可能希望将骰子编号的概念作为构造函数参数添加,并在您的 Start 方法中进行计数管理。

英文:

Each instance of the Dice class has a counter field, since the field is not static. They both have the value 1 as the ShowValue method has been called once for each dice instance.

You might want to add the dice numbering concept as a constructor parameter and manage the counting in your Start method instead.

答案2

得分: 0

你需要在Dice实例之外跟踪骰子的数量,因为每个实例都会有自己的counter局部副本,这两个实例中都从0开始。

Dice类中添加一个构造函数,接受一个int参数来跟踪它是哪个实例。

public class Program
{
    static void Main(string[] args)
    {
        Start();
    }

    static void Start()
    {
        // 在实例的范围之外保存骰子计数器
        int diceCounter = 0;
        Dice d1, d2;
        // 实例化两个骰子,同时递增diceCounter并将值传递给构造函数
        d1 = new Dice(++diceCounter);
        d2 = new Dice(++diceCounter);

        d1.Throw();
        d2.Throw();

        d1.ShowValue();
        d2.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }
}

class Dice
{
    static Random NumberOneToSix = new Random();
    // 包含此实例的ID/实例编号的int字段。
    private readonly int identifier;
    public int value;

    // 接受int参数的构造函数
    public Dice(int identifier)
    {
        this.identifier = identifier;
    }

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
    }

    public void ShowValue()
    {
        Console.WriteLine("骰子 " + identifier + " 的值为: " + value);
    }
}

你可以在这里了解更多关于构造函数的信息。此外,还有关于this关键字readonly关键字的一些信息。

英文:

You need to track the number of die outside of the Dice instances because each instance will have their own local copy of counter which will start at 0 in both instances.

Add a constructor to the Dice class which accepts an int parameter to track the ID of which instance it is.

public class Program
{
    static void Main(string[] args)
    {
        Start();
    }

    static void Start()
    {
        // Hold the Dice Counter outside of the scope of the instances
	    int diceCounter = 0;
        Dice d1, d2;
        // Instantiate both dice, while incrementing the diceCounter and passing the value to the ctor
        d1 = new Dice(++diceCounter);
        d2 = new Dice(++diceCounter);

        d1.Throw();
        d2.Throw();

        d1.ShowValue();
        d2.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }
}

class Dice
{
    static Random NumberOneToSix = new Random();
    // int field which contains the ID/instance number for this instance.
    private readonly int identifier;
    public int value;

    // Constructor which accepts an int parameter
    public Dice(int identifier)
	{
	    this.identifier = identifier;
	}

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
    }

    public void ShowValue()
    {
        Console.WriteLine("The value of dice " + identifier + " is: " + value);
    }
}

You can read more about constructors here

Here is also some information on the this keyword as well as the readonly keyword

答案3

得分: 0

我建议稍微修改你的类。原因是,如果没有像Jonas在他的回答中建议的外部追踪器,你将无法在方法调用之间跟踪骰子的投掷情况。

尝试像这样做:

    static void Main(string[] args)
    {
        Program DungeonDiceMasters = new Program();
        DungeonDiceMasters.Start();
    }

    void Start()
    {
        DiceRoller dr = new DiceRoller();

        dr.Throw();
        dr.ShowValue(true);
        dr.Throw();
        dr.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }


class DiceRoller
{
    static Random NumberOneToSix = new Random();
    public int value = 0;
    public int counter = 0;
    private bool diceThrown = false; //添加投掷追踪器以避免丢失骰子投掷情况

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
        diceThrown = true; //骰子已被投掷
    }

    public void ShowValue(bool startOver = false) //可选参数,用于重置骰子计数器
    {
        if(startOver) //如果设置了参数,重置计数器
            counter = 0;
        counter = counter + 1;
        if(!diceThrown) //不言而喻
            Throw(); //内部调用方法来投掷骰子
        Console.WriteLine("骰子 " + counter + " 的值为: " + value);
        diceThrown = false; //重置骰子投掷追踪器
    }
}

我添加了一些注释,希望能够解释正在发生的事情以及为什么我进行了修改。希望这有所帮助!

英文:

I'd suggest modifying your class just a bit. Reason being, that without some sort of external tracker like Jonas has suggested in his answer, you will not be able to keep track of dice rolls between method calls.

Try something like this:

    static void Main(string[] args)
    {
        Program DungeonDiceMasters = new Program();
        DungeonDiceMasters.Start();
    }

    void Start()
    {
        DiceRoller dr = new DiceRoller();

        dr.Throw();
        dr.ShowValue(true);
        dr.Throw();
        dr.ShowValue();

        Console.Write("\n");

        Console.ReadKey();
        Start();
    }


class DiceRoller
{
    static Random NumberOneToSix = new Random();
    public int value = 0;
    public int counter = 0;
    private bool diceThrown = false; //add throw tracker to avoid losing dice rolls

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
        diceThrown = true; //dice have been thrown
    }

    public void ShowValue(bool startOver = false) //optional parameter to reset dice counter
    {
        if(startOver) //if param set, reset counter
            counter = 0;
        counter = counter + 1;
        if(!diceThrown) // self-explanatory
            Throw(); //internal call to method to throw dice
        Console.WriteLine("The value of dice " + counter + " is: " + value);
        diceThrown = false; //reset dice throw tracker
    }
}

I added some comments to hopefully explain what's going on and why I made the edits I did. Hope this helps!

答案4

得分: 0

你可以使用以下方式为计数器使用静态字段:

public static int counter = 0;

但是每次投掷后,值会改变,所以它是一个投掷计数器,而不是骰子的数量。

输出:

骰子 1 的值为:3
骰子 2 的值为:4

所以你可以使用属性来跟踪骰子的数量和每个骰子的投掷值:

class Dice
{
    static Random NumberOneToSix = new Random();
    public int value;
    public static int diceCounter = 0;
    public int diceNumber = 0;
    public int throwCounter = 0;

    // 在构造方法中设置骰子的ID
    public Dice()
    {
        diceCounter = diceCounter + 1;
        diceNumber = diceCounter;
    }

    public void Throw()
    {
        value = NumberOneToSix.Next(1, 7);
        throwCounter = throwCounter + 1;
    }

    public void ShowValue()
    {
        Console.WriteLine("骰子 " + diceNumber + " 在投掷 #" + throwCounter + " 中的值为:" + value);
    }
}

输出为:

骰子 1 在投掷 #1 中的值为:6
骰子 2 在投掷 #1 中的值为:3
英文:

you can use a static field for the counter as follows:

public static int counter = 0;

but after each throw value is changed so it is a throw counter not the number of dice.
output:

  The value of dice 1 is: 3
  The value of dice 2 is: 4

so you can use properties to save track of the dice count and the thrown number for each :

class Dice
{
	static Random NumberOneToSix = new Random();
	public int value;
	public static int diceCounter = 0;
	public  int diceNumber =0;
	public  int throwCounter = 0;
    //set id of dice in constructor method
	public  Dice(){
			diceCounter=diceCounter+1;
	        diceNumber=diceCounter;
	}
	public void Throw()
	{
		value = NumberOneToSix.Next(1, 7);
		throwCounter=throwCounter+1;
	}

	public void ShowValue()
	{

		Console.WriteLine("The value of dice " + diceNumber +" in throw #"+throwCounter+ " is: " + value);
	}
}

and the output is :

The value of dice 1 in throw #1 is: 6
The value of dice 2 in throw #1 is: 3

huangapple
  • 本文由 发表于 2020年1月3日 22:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定