lundi 17 juin 2019

Random does not work in dotnetfiddle website?

Using random Next() function at the beginning of my program generates a new number every time while block executed. But Next() is outside the while cycle!

This question about website dotnetfiddle.net and random generator. The very simple and obvious program does not work.

// Guess number game

    // NOTE! Works perfect if I use a constant number instead of Random.  
    Random rnd = new Random();
    int number = rnd.Next(1, 100);
    int userInput = 0;
    bool isPlaying = true; 

    while (isPlaying) {
        try {
            userInput = Int32.Parse(Console.ReadLine());
        }
        catch (FormatException e)
        {
            Console.WriteLine(e.Message);               
        }

        if (userInput < number) {
            Console.WriteLine("more");
        } else if (userInput > number) {
            Console.WriteLine("less");
        } else {
            isPlaying = false;
        }
    }

    Console.WriteLine("You win");

https://dotnetfiddle.net/uy3Phc

How to solve this?

I was not able to find any support contacts so I put question here.




Aucun commentaire:

Enregistrer un commentaire