Keresés

Új hozzászólás Aktív témák

  • Zalanius

    tag

    válasz Pörp #8012 üzenetére

    Még egy variáció alább (.net 4.5+). A fenti kettő amúgy jobb válasz, de eljátszottam kicsit a feladattal async eszközökkel, hátha érdekes lehet. Nem a legszebb a "novekvo" ilyen használata, de ennél a példánál belefér az ilyesmi is, nincsenek más konkurrens részek, amikre még tekintettel kellene lenni. A ReadKey hallgatózás hátránya, hogy ha el is értük a maxot, egy ESC akkor is kell, mielőtt a "Kész..." megjelenhet.

    static void Main(string[] args)
    {
    Console.WriteLine("Befejezés: ESC. Irányváltás: SPACE.");
    CancellationTokenSource cts = new CancellationTokenSource();
    const int MAX = 1000;
    int x = 0;
    bool novekvo = true;
    Task t1 = Task.Factory.StartNew(async () =>
    {
    while (!cts.Token.IsCancellationRequested && x < MAX)
    {
    Console.Write(novekvo ? ++x : --x);
    Console.Write(" ");
    await Task.Delay(50);
    }
    }, cts.Token);

    ConsoleKeyInfo keyInfo = Console.ReadKey(true);
    while (keyInfo.Key != ConsoleKey.Escape)
    {
    if (keyInfo.Key == ConsoleKey.Spacebar)
    {
    novekvo = !novekvo;
    }
    keyInfo = Console.ReadKey(true);
    }

    cts.Cancel();
    Console.WriteLine("Kész. A kilépéshez nyomj meg egy gombot.");
    Console.ReadKey();
    }

  • nmate91

    tag

    válasz Pörp #8012 üzenetére

    using System;
    using System.Threading;

    namespace ConsoleApp1
    {
    class Program
    {
    static int Increase(int count)
    {
    do
    {
    while (!Console.KeyAvailable)
    {
    Console.WriteLine($"count: {count++}");
    Thread.Sleep(25);
    }
    } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
    return count;
    }

    static int Decrease(int count)
    {
    do
    {
    while (!Console.KeyAvailable)
    {
    Console.WriteLine($"count: {count--}");
    Thread.Sleep(25);
    }
    } while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
    return count;
    }

    static void Main(string[] args)
    {
    Console.WriteLine("Press Space to change, and ESC to stop");
    do
    {
    int count = 0;
    count = Increase(count);
    count = Decrease(count);
    } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
    }
    }
    }

    Hasonlo, decrease utan megall, ujra spacet nyomva indul, nullarol. Ha nem nullarol kell, csak ki kell tenni eggyel kijjebb a count deklaralast. Ha kell az 1000 hatarnak, csak a do while-nal kondiciokent kikotod.

  • joysefke

    veterán

    válasz Pörp #8012 üzenetére

    Az egyetlen nehézséget az okozza ebben, hogy a Console.ReadKey() blokkol ha éppen nem volt megnyomva billentyű, ekkor addig vár amíg le nem nyomsz egy billenytűt, azt kiolvassa és csak azután megy tovább a végrehajtás. A Console.KeyAvailable segítségével ezt ki lehet küszöbölni, ennek akkor ha true az értéke, akkor volt lenyomva billentyű, amelynek az értéke bufferbe került, ezt ki lehet olvasni a Console.ReadKey()-jel, anélkül, hogy az blokkolna.

    using System;
    using System.Threading;

    // NonblockingReadKey
    class Program
    {
    // N > 1
    static void Count(int N)
    {
    //this will store the pressed key
    ConsoleKeyInfo consoleKey;

    // counter will be the running variable
    // starting to count down from N
    int counter = N;

    // when true: counting down
    // when false counting up
    bool countingDown = true;

    // ends one below zero when counting down and one above N when counting up
    while (!(counter==-1 && countingDown) && !(counter == N+1 && !countingDown) )
    {
    Console.Clear();
    Console.Write(counter);
    Thread.Sleep(250);

    // Console.KeyAvailable == true only if there was a keypress
    //in the console
    if (Console.KeyAvailable)
    {
    // reads the keypress
    consoleKey= Console.ReadKey();
    // reads any remaining keypresses from the buffer
    // if you have pressed more then one keys during sleep-time
    while (Console.KeyAvailable) consoleKey = Console.ReadKey();

    // Immediatelly breaks at reading ESC key
    if (consoleKey.Key == ConsoleKey.Escape)
    { Console.Clear(); break; }

    //switches the counting direction
    // true -> false
    // false -> true
    // countingDown ^= true; ;)
    countingDown = !countingDown;

    }

    //increments or decrements the counter according to
    // the value of countingDown
    if (countingDown == true) { --counter; }
    else { ++counter; }
    }
    Console.Clear();
    Console.WriteLine("Finished!!!");

    }
    static void Main(string[] args)
    {
    Count(100);
    }
    }

Új hozzászólás Aktív témák