سؤال

أرغب في مقارنة المفتاح الذي تم الضغط عليه في وحدة تحكم بمفتاح السهم الأيسر إذا كانت متساوية ، مما يعني أن المفتاح المضغوط هو مفتاح السهم الأيسر ، وتغيير مفتاح لون وحدة التحكم إلى Cyan ...

لست متأكدًا من كيفية إعداد البيان 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";
        }
    }
}

}
هل كانت مفيدة؟

المحلول

جرب هذا:

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

if (keypress.Key == ConsoleKey.LeftArrow)
{
    Console.BackgroundColor = ConsoleColor.Cyan;
}

نصائح أخرى

تحتاج إلى استخدام keypress.Key (بدلاً من .KeyChar) - أيضا لديك "Cyan" يجب ان يكون ConsoleColors.Cyan جدا.

جرب هذا:

    ConsoleKeyInfo keypress;
    keypress = Console.ReadKey(); // read keystrokes 
    if ( (int)keypress.Key == (char)ConsoleKey.LeftArrow )
    {
        Console.BackgroundColor = ConsoleColor.Cyan;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top