Counting until a specific number and start again + backwards

255 Views Asked by At

The thing is, I want to be able to increase/decrease a variable (int) by pressing up and down arrow keys. But how do I manipulate the variable, so it goes from 3 to 1 and backwards from 1 to 3 again?

I'm using Visual C# express 2010 and it is a Windows Console application! Sorry for the trouble!

I'm desperately trying to get into C# and am struggling with such basic things. I'd be very grateful if someone could help me with this. I've got this far, this should become a menu on where the user can scroll through three options: 1- New Game // 2- Load Game and 3- Exit Game

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int iMOP = 0;

            ConsoleKeyInfo keyInfo = Console.ReadKey();
            if (keyInfo.Key == ConsoleKey.UpArrow){
            }
            else if (keyInfo.Key == ConsoleKey.DownArrow){
            }

                switch (iMOP)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
        }
    }
}

Additional: I'll try to refresh the menu with Console.Clear, though I'll have to figure the counting issue. I've translated it into this now: AND IT WORKS NOW THANKS FOR THE INPUT, GUYS!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.CursorVisible = false;

            int iMOP = 1;

            Console.WriteLine(" >>New Game");
            Console.WriteLine("   Load Game");
            Console.WriteLine("   Exit Game");

            while (iMOP != 5)
            {
                {
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key == ConsoleKey.UpArrow)
                    {
                        iMOP--;
                    }
                    else if (keyInfo.Key == ConsoleKey.DownArrow)
                    {
                        iMOP++;
                    }
                }

                if (iMOP == 0)
                {
                    iMOP = 3;
                }
                else if (iMOP == 4)
                {
                    iMOP = 1;
                }

                switch (iMOP)
                {
                    case 1:
                        Console.Clear();
                        Console.WriteLine(" >>New Game");
                        Console.WriteLine("   Load Game");
                        Console.WriteLine("   Exit Game");
                        break;
                    case 2:
                        Console.Clear();
                        Console.WriteLine("   New Game");
                        Console.WriteLine(" >>Load Game");
                        Console.WriteLine("   Exit Game");
                        break;
                    case 3:
                        Console.Clear();
                        Console.WriteLine("   New Game");
                        Console.WriteLine("   Load Game");
                        Console.WriteLine(" >>Exit Game");
                        break;
                }
            }
        }
    }
}
1

There are 1 best solutions below

2
On

To "loop" the numbers; replace:

if (iMOP == 0)
{
   iMOP = 3;
}
else if (iMOP == 4)
{
    iMOP = 1;
}

with:

iMOP = (iMOP % 3) + 1;

% returns the remainder after division; so it can only return 0, 1, or 2. Then you add 1 to get the range 1, 2, and 3. Note this trick is exactly the same as the one you use to scale a random double:

int scaledRandom = (rand.NextDouble() % max) + min;