2

コンソールで押されたキーが等しい場合、つまり押されたキーが左矢印キーであった場合、コンソールの背景色をシアンに変更して、キーを比較したいと思います。

ただし、コンソールでキーを比較する方法がわからないため、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";
        }
    }
}

}
4

3 に答える 3

4

これを試して:

ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes 

if (keypress.Key == ConsoleKey.LeftArrow)
{
    Console.BackgroundColor = ConsoleColor.Cyan;
}
于 2010-07-02T21:06:10.313 に答える
1

keypress.Key(の代わりに)を使用する必要があり.KeyCharます-また、使用する"Cyan"必要がありConsoleColors.Cyanます。

于 2010-07-02T21:07:35.210 に答える
0

これを試して:

    ConsoleKeyInfo keypress;
    keypress = Console.ReadKey(); // read keystrokes 
    if ( (int)keypress.Key == (char)ConsoleKey.LeftArrow )
    {
        Console.BackgroundColor = ConsoleColor.Cyan;
    }
于 2010-07-02T21:10:34.950 に答える