问题是,我希望能够通过按向上和向下箭头键来增加/减少变量(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刷新菜单,但我必须计算计数问题。 我现在把它翻译成了这个:它现在感谢输入,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;
}
}
}
}
}
答案 0 :(得分:1)
To" loop"数字;替换:
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;