Home > Mobile >  (C#) How do I stop Increment and decrement jumping around place values?
(C#) How do I stop Increment and decrement jumping around place values?

Time:02-25

For some reason this code makes the numbers jump around in place value.

For an ex.

If I add the numbers up to up to 10 when I subtract one time it will change to 90 if I subtract again it goes to 80. If I start adding again 90 turns into 01...

So somewhere in my code I must be telling it to do this...

I have tried a few different things but the same thing keeps happening.

int x;
int y;

Console.WriteLine("X:"   (x=0)   "Y:"   (y=0));

//Change Coordinates
while(true){
  ConsoleKey key = Console.ReadKey().Key;
  Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop -1);
Console.WriteLine("X:" x  "Y:" y);

if (key == ConsoleKey.UpArrow){

    y=y 1;
 }
else if (key == ConsoleKey.DownArrow){

        y=y-1;
 }
}

CodePudding user response:

You need

Console.WriteLine($"X:{x,2} Y:{y,2}");

You were not printing y for a start. But your main problem was that you were leaving old characters in the line once you had got to 2 digits. THis forces the output to be always 2 digits wide

  • Related