Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

c# - Problem with DotNetFiddle and Console.ReadLine that causes a Re-run when pressing ENTER key

I have a weird scenario using a C# online compiler with this code:

int randomNumber;
int guess;
int countGuess;
bool correctGuess = false;
bool alreadyCalled = false;
int lastGuess;

public void Main()
{
    if(!alreadyCalled)
    {
        RandomNumberGenerator();
        alreadyCalled = true;
    }
    GuessWhatNumber();
}

private void GuessWhatNumber()
{
    while(!correctGuess)
    {
        Console.WriteLine("Guess the number : ");
        guess = Convert.ToInt32(Console.ReadLine());
        
        if(guess > randomNumber)
        {
            Console.WriteLine("Too High");
            correctGuess = false;
        }
        else if(guess < randomNumber)
        {
            Console.WriteLine("Too Small");
            correctGuess = false;
        }
        else    
        {
            Console.WriteLine("Correct!");
            correctGuess = true;
            countGuess = 0;
            Environment.Exit(0);
        }
        
        if(lastGuess != guess)
        {
            countGuess+=1;
            Console.WriteLine("Count of Guess : " + countGuess);
        }
        else
        {
            Console.WriteLine("Count of Guess is Still: " + countGuess);
        }
        lastGuess = guess;
    }
}

private void RandomNumberGenerator()
{
    Random rand = new Random();
    randomNumber = rand.Next(50);
    Console.WriteLine("Number To Guess : " + randomNumber); 
}

The Random Number Generator method must be called only once but on that online compiler it keeps calling and calling the method all over again. What am I doing wrong here? Or is it the compiler?

Dotnetfiddle.net settings are:

  • C#
  • Console
  • Compiler: .NET 4.7.2
  • No nugets packages
  • Autorun: NO
question from:https://stackoverflow.com/questions/65918816/problem-with-dotnetfiddle-and-console-readline-that-causes-a-re-run-when-pressin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Once you run the code it is send to the server as request and it'll return the results as text to display in the interface. Once it encounters a point where it requires the users input instead of waiting for the input the full request is send again, but this time with the information from the input window. Then on the backend the full request runs again and automatically adds the input from the users as far as it's known.

In most cases that would be no problem because the same script usually yields the same results. This is a different case however because you added a random element into the script. So each time you input new data everything that happened before runs again and may yield different results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...