Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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# 用另一个数组随机分配数组值';s值_C#_Arrays_List_Random - Fatal编程技术网

C# 用另一个数组随机分配数组值';s值

C# 用另一个数组随机分配数组值';s值,c#,arrays,list,random,C#,Arrays,List,Random,如何将一组数组值与另一组数组值赋值?两者都有26个值 我正在模拟一个交易或无交易游戏,用户从指定列表中选择一个案例。现在,在每次运行console应用程序时,我希望每个案例的每个现金奖励都有一个随机分配(为了公平起见)。我的代码如下所示: int[] cashPrizeArray = new int[] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000,

如何将一组数组值与另一组数组值赋值?两者都有26个值

我正在模拟一个交易或无交易游戏,用户从指定列表中选择一个案例。现在,在每次运行console应用程序时,我希望每个案例的每个现金奖励都有一个随机分配(为了公平起见)。我的代码如下所示:

    int[] cashPrizeArray = new int[] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
    string[] caseArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26" };

    Console.WriteLine("Deal or Not!");
    Console.Write("Choose a case: 1-26: ");
    string userCase = Console.ReadLine();



    if (!caseArray.Contains(userCase))
    {
        Console.WriteLine("\nUnexpected input text.\nThis application will now be terminated.\nPress ENTER to continue...");
        Console.ReadLine();
        Environment.Exit(0);
    }

    else
    {
        Console.WriteLine("You chose case " + userCase);
        Console.ReadLine();
    }

当用户选择这些案例时,我需要一个接一个地引用它们,然后在最初打开后将它们从数组中移除。

如果您制作一个二维列表,第一个维度显示案例,第二个维度显示该金额的金额。然后你检查你是否已经有了 使用该特定案例,如果是,则重试(使用goto,但您可以更改),或者如果未使用该案例,则将该案例与第二维度中的金钱一起添加到列表中

代码:

public static List PairedCases=new List();
公共静态void addsubstrin(字符串money、字符串casenom)
{
列表子列表=新列表();
子列表。添加(货币);
子列表添加(casenom);
PairedCases.Add(子列表);
}
静态void Main(字符串[]参数)
{
int[]cashPrizeArray=新的int[]{0,1,2,5,10,20,50,100,150,200,250,500,750,1000,2000,3000,4000,5000,10000,15000,20000,25000,50000,75000,100000,200000};
字符串[]caseArray=新字符串[]{“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”、“14”、“15”、“16”、“17”、“18”、“19”、“20”、“21”、“22”、“23”、“24”、“25”、“26”};
随机rnd=新随机();
foreach(现金普里兹数组中的int inte)
{
重新启动:;
int putincase=rnd.Next(1,27);
bool-found=false;
//在这里,我们查查它是否已经在列表中
对于(int a=0;a
你有问题吗?在这里搜索
shuffle c#
几十个答案,等待你找到them@Enigmativity如何将一组数组值与另一组数组值赋值?两者都有26个值。@kzone95:我甚至不清楚“用另一个数组的值指定一组数组值”是什么意思。到目前为止,您的程序有两个数组,并接受用户的一个输入值。在那个代码中有什么东西不起作用吗?如果您的代码正在运行,并且您希望进入下一步,那么这一步是什么?从逻辑上把问题分解成小而具体的部分。下一步你要试哪一块?您尝试了什么?在类中定义一个字段
private static Random\u rnd=new Random()
然后在定义
cashPrizeArray
后的行中放置以下内容:
cashPrizeArray=cashPrizeArray.OrderBy(x=>\u rnd.Next()).ToArray()
public static List<List<string>> PairedCases = new List<List<string>>();

    public static void addsubstrin(string money, string casenom)
    {
        List<string> sublist = new List<string>();

        sublist.Add(money);
        sublist.Add(casenom);

        PairedCases.Add(sublist);
    }

    static void Main(string[] args)
    {
        int[] cashPrizeArray = new int[] { 0, 1, 2, 5, 10, 20, 50, 100, 150, 200, 250, 500, 750, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 50000, 75000, 100000, 200000 };
        string[] caseArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26" };

        Random rnd = new Random();

        foreach (int inte in cashPrizeArray)
        {
            Restart:;

            int putincase = rnd.Next(1, 27);

            bool found = false;

            //here we chack if its already in the list

            for (int a = 0; a < PairedCases.Count(); a++)
            {
                if (putincase.ToString() == PairedCases[a][1])
                {
                    found = true;
                }
            }

            if(found == false)
            {
                addsubstrin(inte.ToString(), putincase.ToString());
            }
            else
            {
                goto Restart;
            }
        }

        for (int i = 0; i < PairedCases.Count(); i++)
        {
            Console.WriteLine(PairedCases[i][0] + "   " + PairedCases[i][1]);
        }

        Console.ReadLine();
    }