class menu
{
string[] elements;
public menu(string[] ele) { elements = ele; }
public menu() : this(new string[3]) { }
public void dispMenu(int sel)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
for (int i = 0; i != elements.Length; i++)
{
if (i != sel) { Console.WriteLine(" " + elements[i]); }
else { Console.WriteLine(">" + elements[i] + "<"); }
}
}
public int doMenu()
{
int selection = 0;
do
{
dispMenu(selection);
while (!Console.KeyAvailable)
{
}
if (Console.ReadKey().Key == ConsoleKey.UpArrow)
{
if (selection == 0) { selection = elements.Length; }
else { selection--; }
}
else if (Console.ReadKey().Key == ConsoleKey.DownArrow)
{
if (selection == elements.Length) { selection = 0; }
else { selection++; }
}
} while (Console.ReadKey(true).Key != ConsoleKey.Enter);
return selection;
}
}
class Program
{
static void Main(string[] args)
{
menu swag = new menu(new string[3] {"Swag", "Money", "yolo"});
Console.WriteLine(swag.doMenu());
Console.Read();
}
}
爲什麼這個掛斷?這是一個菜單對象,應該向上和向下箭頭作出響應,並在按下輸入時終止,並返回選擇。運行時它可以工作,但是您必須多次按下該鍵才能註冊。回車鍵也表現這種方式。這爲什麼掛斷?菜單程序
我懷疑這是問題,但是您的代碼頂部有一個無關的「{」(第1行)。 –
不是這個問題,那只是從代碼粘貼到這個論壇 –