数到一个特定的数字,然后重新开始+倒退



问题是,我希望能够通过按向上和向下箭头键来增加/减少变量(int)。但是我如何操作变量,让它从 3 到 1,然后再次从 1 倒退到 3?

我正在使用Visual C#Express 2010,它是一个Windows控制台应用程序!很抱歉给您带来麻烦!

我拼命地尝试进入 C#,并且正在为这些基本的事情而苦苦挣扎。如果有人能帮助我解决这个问题,我将不胜感激。我已经做到了这一点,这应该成为一个菜单,用户可以在其中滚动浏览三个选项:1-新游戏//2-加载游戏和3-退出游戏

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;
                }
        }
    }
}

附加:我将尝试使用Console.Clear刷新菜单,尽管我必须弄清楚计数问题。我现在已将其翻译成: 它现在可以工作了,感谢您的输入,伙计们!

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;
                }
            }
        }
    }
}

要"循环"数字;请替换:

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

跟:

iMOP = (iMOP % 3) + 1;

% 返回除法后的余数;因此它只能返回 0、1 或 2。然后添加 1 以获得范围 1、2 和 3。请注意,此技巧与您用于缩放随机双精度的技巧完全相同:

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

最新更新