samedi 19 juin 2021

Trying to end a while loop once a 1 is randomly generated or it loops 10 times

(I apologize for the atrocious formatting)

I am attempting to make a random number generator (min = 1, max = 10) that loops until either

a) a 1 is generated (serve as primary sentinel). b) loop iterates 10 times (secondary sentinel) without generating 1.

I am looking for something like this:

6 3 8 4 1 "A one was received in 5 attempts,"

or

3 8 5 7 2 2 4 8 8 3 "A one was never received."

but the problem is that the loop continues even if a 1 is generated, and the "A one was never recieved." message displays. The loop is as follows:

**while (randomNumber.Next() != 1 || loopCounter <= 10));

{

loopCounter++;

Console.WriteLine(randomNumber.Next(10));

Thread.Sleep(100);

if (randomNumber.Next() == 1)

{

Console.WriteLine($"A one was recieved in {loopCounter} attempts.")

break;

}

if (loopCounter <= 10)

{

Console.WriteLine("A one was not recieved.") break;**

giving something like

1 4 6 9 7 1 3 3 1 9 "A one was not recieved."




Aucun commentaire:

Enregistrer un commentaire