-1

I'm trying to set my code for counting math games, all math operations are fine except for division. I don't know how to set the while loop correctly. There is a problem with division, such that I would like the division to be residual, so I came up with one method which is given below. It is all in WPF Application. I would like to count only single-digit numbers.

Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, firstNumber);
while (firstNumber % secondNumber != 0);
  {
  secondNumber++;
  }
total = firstNumber / secondNumber;

Why does it still show me the values ​​that have a residual division?

Thank you for any advice

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • 2
    Remove the semicolon at the end of your `while` statement (this terminates the loop immediately) – r3mainer May 24 '21 at 11:38
  • 1
    You should learn how to single-step code in the debugger, and then you'd see the issue pretty quickly! Also, Visual Studio should be showing a [CS0642](https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0642?f1url=%3FappId%3Droslyn%26k%3Dk(CS0642)) warning for the extraneous semicolon. – Matthew Watson May 24 '21 at 11:41

1 Answers1

4

The semi colon at the end of line:

while (firstNumber % secondNumber != 0);

...ends the while loop. The code in the remaining block is executed without any condition (as it in fact is a anonymous block):

{
    secondNumber++;
}
kaffekopp
  • 2,551
  • 6
  • 13