In: Computer Science
Design an application that uses the C# random generator to randomly roll a six face dice ( no 1 - no 6) . If the user rolled a 3, the user gets to roll the six face die again. You must use a switch statement to write the result /s of the random dice roll/s to the screen. Hint: You should have nested switches in your program
Using:
Console App(.NET Framework)
using System;
namespace diceRoll
{
class Program
{
static void Main(string[] args)
{
//Random class to generate random number
Random random = new Random();
//To roll first time
int dice1 = random.Next(1, 7);
//Using switch case for each number on the dice
switch (dice1)
{
//If value on the dice is 1, then printing the value.
case 1:
Console.WriteLine("The rolled number on dice is {0}", dice1);
break;
//If value on the dice is 2, then printing the value.
case 2:
Console.WriteLine("The rolled number on dice is {0}", dice1);
break;
//If value on the dice is 3, then we need to get six value.
case 3:
Console.WriteLine("The rolled number on dice is {0}", dice1);
Console.WriteLine("The user gets to roll the six face die
again");
//Rooling the dice second time, to get value 6
int dice2 = random.Next(1, 7);
//if dice2 value is not 6 then rolling again till 6 occurs
while (dice2 != 6)
{
Console.WriteLine("Ocuured value is {0}",dice2);
Console.WriteLine("It's not 6. So roll again till six
occurs");
dice2 = random.Next(1, 7);
}
//Using switch case for dice2
switch (dice2)
{
//And the only case is 6
case 6:
Console.WriteLine("Six is occured.Done!");
break;
}
break;
//If value on the dice is 4, then printing the value.
case 4:
Console.WriteLine("The rolled number on dice is {0}", dice1);
break;
//If value on the dice is 5, then printing the value.
case 5:
Console.WriteLine("The rolled number on dice is {0}", dice1);
break;
//If value on the dice is 6, then printing the value.
case 6:
Console.WriteLine("The rolled number on dice is {0}", dice1);
break;
}
Console.ReadKey();
}
}
}