我想将控制台中按下的键与左箭头键进行比较,如果它们相等,则表示按下的键是左箭头键,键将控制台的背景颜色更改为青色...
我不知道如何设置If语句,因为我不知道如何比较控制台中的键。
using System;
namespace ConsolePaint
{
class MainClass
{
public static void Main (string[] args)
{
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if ( keypress.KeyChar == ConsoleKey.LeftArrow )
{
Console.BackgroundColor = "Cyan";
}
}
}
}
答案 0 :(得分:4)
试试这个:
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if (keypress.Key == ConsoleKey.LeftArrow)
{
Console.BackgroundColor = ConsoleColor.Cyan;
}
答案 1 :(得分:1)
您需要使用keypress.Key
(而不是.KeyChar
) - 您的"Cyan"
也应该ConsoleColors.Cyan
。
答案 2 :(得分:0)
试试这个:
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if ( (int)keypress.Key == (char)ConsoleKey.LeftArrow )
{
Console.BackgroundColor = ConsoleColor.Cyan;
}