Visual Studio C# Console application not always working

Updated on July 1, 2017 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
5 on June 30, 2017

I made a simple loop with while and random numbers to show how many loops but sometimes nothing appears or just the random numbers appear i also made a goto to the begining but it works 1 or 2 times. Is the problem in the computer because i have had similar problems before

GT:
Random g = new Random();
int x = g.Next(0, 100);
int y = g.Next(0, 100);
int b = 0;
int z = y;

while (y != x)
{
if (y < x)
{
y++;
b++;

}
if (y > x)
{
y–;
b++;

}
}
Console.WriteLine(“x =” + x + “\ny =” + z + “\nb =” + b);
Console.ReadKey();
goto GT;

 

  • Liked by
Reply
1 on June 30, 2017

We need more information. Post your code. Also never use goto (labels).

 

on June 30, 2017

here is the code 1 out of 3 times the console is empty and i dont know what is the problem

 

Show more replies
  • Liked by
Reply
Cancel
1 on June 30, 2017

This is really wrong, firstly, what is this:

–y

That character is what? Did you possibly mean –?

The program works as expected, do you even know how this works?

on June 30, 2017

There are two – (minuses) they just merged and you cant see them here it subtracts 1 from y(the same as y- -)  and yes i know how it works it works just sometimes it dosent load for me properly and i asked if it was from the PC because it is old and barely works

Show more replies
  • Liked by
Reply
Cancel
0 on July 1, 2017

Here is the fixed code, and I also switched to a while loop instead of goto:

    while (true)
        {
            Random g = new Random();
            int x = g.Next(0, 100);
            int y = g.Next(0, 100);
            int b = 0;
            while (y != x)
            {
                if (y < x)
                {
                    y++;
                    b++;
                }
                if (y > x)
                {
                    y--;
                    b++;
                }
            }
            Console.WriteLine("x =" + x + "\ny =" + (y + b) + "\nb =" + b);
            Console.WriteLine();
            Console.ReadKey();
        }

 

  • Liked by
Reply
Cancel