Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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#_Asp.net - Fatal编程技术网

C# 从设置为类的固定数组生成随机值?

C# 从设置为类的固定数组生成随机值?,c#,asp.net,C#,Asp.net,我们正在做一个学校项目,一个学习游戏,我们正在使用一个从堆栈中生成值的类。我们的问题是,堆栈的值用完后,游戏/程序崩溃并停止 我想将其更改为一个数组,其中包含固定值和单元格,每次寻址时都会给出一个随机值 现在的代码是: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for CellIn

我们正在做一个学校项目,一个学习游戏,我们正在使用一个从堆栈中生成值的类。我们的问题是,堆栈的值用完后,游戏/程序崩溃并停止

我想将其更改为一个数组,其中包含固定值和单元格,每次寻址时都会给出一个随机值

现在的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
    System.Collections.Generic.Stack<Cell> _stack = new Stack<Cell>();



    public CellProducer()
    {



        _stack.Push(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 });
        _stack.Push(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 });
    }

    public Cell ProduceNextCell()
    {
        return _stack.Pop();
    }
}
知道我如何保留类的名称,但只更改生成随机值的数组的内部吗

顺便说一句,cell是我们制作的另一个类,数组应该由他制作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for Cells
/// </summary>
public class Cell:Panel //מחלקת משושים
{
    protected string _instrumentName; //שם כלי הנגינה  //
    protected int _insturmentFamily; //סוג כלי הנגינה - המשפחה //
    protected int _myPlayer; //ערכים מספריים של 0 - אין שחקן - 1 ו2 לפי השחקנים

    public string InstrumentName
    {
        get
        {
            return _instrumentName;
        }

        set
        {
            _instrumentName = value;
        }
    }

    public int InstrumentFamily
    {
        get
        {
            return _insturmentFamily;
        }

        set
        {
            _insturmentFamily = value;
        }
    }

    public int myPlayer
    {
        get
        {
            return _myPlayer;
        }

        set
        {
            _myPlayer = value;
        }
    }
}

public class CellPosition // מחלקה ששומרת את מיקומי התאים
{
    private int _Column;
    private int _Row;

    public int Column
    {
        get
        {
            return _Column;
        }

        set
        {
            _Column = value;
        }
    }

    public int Row
    {
        get
        {
            return _Row;
        }

        set
        {
            _Row = value;
        }
    }
}

谢谢你的帮助,很抱歉所有的便条都是希伯来语

您可以使用列表或数组来存储仪器。然后生成一个介于0和元素数之间的随机整数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
    System.Collections.Generic.List<Cell> cells = new List<Cell>();
    int index = -1;

    public CellProducer()
    {
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?'??", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???'?", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "?????? ???", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "??? ???", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 });
    }

    public Cell ProduceNextCell()
    {
        index++;
        if(index>cells.length){
            index = 0;
        return cells[index];
    }
}

我还没有测试过,但我认为它会起作用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CellInfoRepository
/// </summary>
public class CellProducer
{
    System.Collections.Generic.List<Cell> cells = new List<Cell>();
    Random random = new Random();

    public CellProducer()
    {
        cells.Add(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 });
        cells.Add(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 });
    }

    public Cell ProduceNextCell()
    {
        return cells[random.Next(cells.Count)];
    }
}

你使用堆栈而不是列表有什么原因吗?我们一直使用堆栈只是为了让游戏正常运行。我们知道我们需要把它改成某种数组,这样我们得到的值将是随机的。这就是我们试图做的,从信元中生成一个数组,并生成一个随机值:cell[]myArry=newcell[23];这就是我想要做的:见下面尼古拉·马尔科维诺维奇的答案。但请注意,他的答案每次都会给出一个随机值。如果你想要一个顺序列表,就像你已经编码的那样,你可以使用我现在编辑的答案中的代码。首先,非常感谢你的帮助,因为这是一个游戏,我们希望工具名称是随机的、不同的、无休止的,这样它们就可以永远重复。