In: Computer Science
Create a dice simulator in C#. The simulator must roll the dice 6 times and it must add the 3 highest rolls. So say it simulates: 4 4 5 2 1 3 the total would be 13 (4+4+5=13). Repeat this 6 different times.
So if the random dice simulator simulates the outcomes below it should output: 13, 13, 14, 12, 16, 12
1) 4 4 5 2 1 3
2) 2 3 6 1 3 4
3) 5 3 1 4 6 2
4) 4 2 2 1 1 6
5) 5 4 5 6 3 1
6) 1 2 4 5 3 2
C# code
============================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace diceroll
{
class Program
{
static void Main(string[] args)
{
int []arr = new int[6];
int sum=0;
int count = 0;
Random random = new Random();
while (count < 6)
{
for (int i = 0; i < 6; i++)
{
arr[i] = random.Next(1, 7);
//Console.WriteLine(arr[i]);
}
//sort array
Array.Sort(arr);
sum = 0;
for (int i = 3; i < 6; i++)
sum = sum + arr[i];
Console.WriteLine("Dice roll Sum is:{0}", sum);
count++;
}
Console.WriteLine("Press Enter to continue ");
Console.ReadLine();
}
}
}
============================================================================================
Output