Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:为什么我的列表不打印这些项目?_C#_List_Random_Printing - Fatal编程技术网

C# C:为什么我的列表不打印这些项目?

C# C:为什么我的列表不打印这些项目?,c#,list,random,printing,C#,List,Random,Printing,我试图打印列表中的int项,但得到了以下输出,这显然不是我想要打印的: YSolution.Dice YSolution.Dice YSolution.Dice YSolution.Dice YSolution.Dice 源代码: class Dice { //defining variable and properties private bool _hold = false; private Random rnd; public int Current {

我试图打印列表中的int项,但得到了以下输出,这显然不是我想要打印的:

YSolution.Dice
YSolution.Dice
YSolution.Dice
YSolution.Dice
YSolution.Dice
源代码:

class Dice
{
    //defining variable and properties
    private bool _hold = false;

    private Random rnd;
    public int Current { get; private set; }

    public bool IsHolding
    {
        get { return _hold; }
        set { _hold = value; }
    }

public Dice()
    {
        Current = 0;
        rnd = new Random(Guid.NewGuid().GetHashCode());
        FRoll();
    }

    public int FRoll()
    {
        Current = rnd.Next(1, 7);
        return Current;
    }



class DiceCup
    {
        public List<Dice> Dices { get; } = new List<Dice>();

        public DiceCup(int count)

        {
            for (int i = 0; i < count; i++)
            {
                Dices.Add(new Dice());
            }

            foreach (Dice aDice in Dices)
            {
                Console.WriteLine(aDice);
            }
        }
 class Program
{
    static void Main(string[] args)
    {

        DiceCup nbd = new DiceCup(count);



    }
}

方法失败;当一个新项目被添加到列表中时,由于某种原因似乎没有在骰子类中被调用。我真的只想打印列表骰子中的项目,但我没有得到我想要的输出/结果。谁能发现错误

当前,您只需在骰子对象上调用ToString。由于您尚未重写ToString,因此仅使用默认的object.ToString实现,该实现在您的情况下返回对象YSolution.Dice的类型名

您的骰子上有一个返回骰子值的当前属性,如果调用此方法,则该属性将返回骰子值,然后可以打印:change Console.WriteReadice;到Console.WriteReadice.Current

或者,正如其他人指出的,在骰子类上返回骰子的当前值:

class Dice
{
    //defining variable and properties
    private bool _hold = false;

    private Random rnd;
    public int Current { get; private set; }

    public bool IsHolding
    {
        get { return _hold; }
        set { _hold = value; }
    }

    public Dice()
    {
        Current = 0;
        rnd = new Random(Guid.NewGuid().GetHashCode());
        FRoll();
    }

    public int FRoll()
    {
        Current = rnd.Next(1, 7);
        return Current;
    }

    public override string ToString()
    {
        return Current.ToString();
    }
}

您希望实现ToString方法:

公共字符串ToString { 返回Current.ToString; } 除了覆盖ToString方法(如其他答案中所述),您还可以从骰子中收集结果并打印这些结果:

foreach int结果为骰子。选择d=>d。当前 { Console.WriteLineresult; }
该方法在System.Linq命名空间中定义。

Console.WriteLine正在使用骰子中的ToString打印类名和位置。可以重写Dice类中的ToString方法以返回当前属性。可以在这里找到重写ToString的文档。