I have a program that asks the user how many dice they want to roll and then rolls them. Each dice has 6 sides so only possible outcomes are 1-6. Currently everything is working as it should but I would like to make the user have to input the amount of sides the dice have, so instead of the random function being .Next(1, 7);, it would be .Next(1, amountSides); where amountSides stands for the amount of sides the dice have. I have managed to do it without using a method but my professor wants me to use one, but I can’t for the life of me figure it out.
namespace ConsoleApp3
{
class Program
{
static int RollDice(Random randomObject, int amountSides)
{
amountSides = 0;
int random = randomObject.Next(1, amountSides);
return random;
}
static void Main()
{
Random random = new Random(); slumptal
List<int> dices = new List<int>();
Console.WriteLine("\n\tWelcome to the dice generator!");
bool run = true;
while (run)
{
Console.WriteLine("\n\t[1] Roll dice\n" +
"\t[2] Check what you rolled\n" +
"\t[3] Quit");
Console.Write("\tChoose: ");
int choice;
int.TryParse(Console.ReadLine(), out choice);
switch (choice)
{
case 1:
Console.Write("\n\tHow many dices do you want to roll: ");
bool input = int.TryParse(Console.ReadLine(), out int amount);
Console.Write("\n\tHow many dices do you want to roll: ");
int amountSides = Convert.ToInt32(Console.ReadLine());
if (input)
{
for (int i = 0; i < amount; i++)
{
// här kallar vi på metoden RullaTärning
// och sparar det returnerade värdet i
// listan tärningar
dices.Add(RollDice(random, amountSides));
}
}
break;
case 2:
int sum = 0;tärningsrullningar.
if (dices.Count <= 0)
{
Console.WriteLine("\n\tThere are no saved dice rolls! ");
}
else
{
Console.WriteLine("\n\tRolled dice: ");
foreach (int dice in dices)
{
Console.WriteLine("\t" + dice);
sum += dice;
}
Console.WriteLine("\n\tAverage of rolled dices: " + sum / dices.Count);
}
break;
case 3:
Console.WriteLine("\n\tThank you for rolling dice");
Thread.Sleep(1000);
run = false;
break;
default:
Console.WriteLine("\n\tChoose 1-3 from the menu");
break;
}
}
}
}
}
I don’t know how to "transfer" the amountSides variable to the method so that it can perform the calculation and return the correct random number.
I also want to add that I have succesfully done it without using a method but as stated previously, my professor demands a method to be used for this. Here is the same thing done without using a method.
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
Random random = new Random();
List<int> dices = new List<int>();
Console.WriteLine("How many dice do you want to roll:");
bool input = int.TryParse(Console.ReadLine(), out int amount);
Console.WriteLine("How many sides do the dices have:");
int amountSides = Convert.ToInt32(Console.ReadLine());
if (input)
{
for (int i = 0; i < amount; i++)
{
dices.Add(random.Next(1,amountSides));
}
foreach(int dice in dices)
{
Console.WriteLine(dice);
}
}
}
}
}
>Solution :
Remove amountSides = 0;. Don’t override method parameters.
Each dice has 6 sides so only possible outcomes are 1-6
Then you don’t need a parameter, or amountSides at all, so there is nothing to "transfer"
static int RollDice(Random randomObject)
{
return randomObject.Next(1, 7);
}
…
dices.Add(RollDice(random));
Otherwise, if you want other sized dice, you ask for the number of sides, (as you had already done)…
And you use that parameter (as you did), but do not override it
static int RollDice(Random randomObject, int sides)
{
return randomObject.Next(1, sides);
}
…
Console.Write("\n\tHow many dices do you want to roll: ");
bool input = int.TryParse(Console.ReadLine(), out int amount);
Console.Write("\n\tHow many sides does each die have: ");
int amountSides = Convert.ToInt32(Console.ReadLine());
if (input)
{
for (int i = 0; i < amount; i++)
{
dices.Add(RollDice(random, amountSides));
}
}