0

I'm curious to know how you would convert the following into a while loop. I know it doesn't make the most sense in this situation but I am working out of a student workbook and would like to see another example. Thanks in advance, any help is much appreciated.

int sides = 0; 
int number = 0; 
int total = 0; 

Random random = new Random(); 
Console.WriteLine("Choose the number of sides."); 
sides = int.Parse(Console.ReadLine());
Console.WriteLine("Choose number of dice.");
number = int.Parse(Console.ReadLine());

for (int i = 0; i < number; i++) 
{
   int die = random.Next(1, sides); 
   Console.WriteLine("You Rolled {0}", die.ToString());d
   total += die;
}

Console.WriteLine("Your total is {0}", total);
Console.ReadLine();

2 Answers 2

4
int i = 0;
while(i < number)
{
       int die = random.Next(1, sides); 
       Console.WriteLine("You Rolled {0}", die.ToString());
       total += die;
       i++
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was fast. I was typing the "w" in while in my answer when your answer showed up
0

You want to convert a for-loop to a while-loop if I understand correctly.

int currentNum = 0;
while (currentNum < number)
{
   int die = random.Next(1, sides);
   Console.WriteLine("You Rolled {0}", die.ToString());
   total += die;

   currentNum++;
}

1 Comment

There is an error in your code. There is a "d" alone at the end of the 5th line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.