将两个随机生成的数字相加(使用数组)

本文关键字:数字 数组 两个 随机 | 更新日期: 2023-09-27 18:16:29

class Program
{
    const int ROLLS = 51;
    static void Main(string[] args)
    {
        Random r = new Random();
        int sum = 0;
        int[] dice1 = new int[ROLLS];
        int[] dice2 = new int[ROLLS];
        for (int roll = 0; roll <= 50; roll++)
        {
            dice1[roll] = GenerateNum(r);
            dice2[roll] = GenerateNum(r);
            Console.WriteLine("ROLL{0}: {1} + {2} = sum goes here", roll+1, dice1[roll]+1, dice2[roll]+1);
        }
    }
    static int GenerateNum (Random r)
    {
        return r.Next(1, 7);
    }
  }
}

所以我有两个数组来存储随机生成的两个不同的int值,我想要实现的是这两个随机生成的int值的和。

执行后应该显示:第一次掷出:(随机数)+(随机数)=(两个随机数之和)

将两个随机生成的数字相加(使用数组)

只需将两者相加并存储在sum中。然后以与在控制台输出中显示其余值相同的方式显示sum:

dice1[roll] = GenerateNum(r);
dice2[roll] = GenerateNum(r);
sum = dice1[roll] + dice2[roll];
Console.WriteLine("ROLL{0}: {1} + {2} = {3}", roll + 1, dice1[roll], dice2[roll], sum);