Hirdetés
- Alaposan felpumpálta AI gyorsítójának izmait a Google
- Kisebb "atomreaktort" avatott a Minisforum
- A Microsoft bedurvult, így a jövőben keményen odacsapnak a veszélyes drivereknek
- Scalperektől félti RAM moduljait a Framework
- Drasztikusan lassíthatja a játékokat egyes VGA-kon a Windows 11 új frissítése
Új hozzászólás Aktív témák
-
joysefke
veterán
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.KeyAvailablesegí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
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Xiaomi Redmi 14C / 4/128GB / Kártyafüggetlen / 12Hó Garancia
- HP 24" IPS LED FULL HD monitor 24fh 4HZ37AA
- CTO Bontott 0perces TÜZES EZÜST MacBook Air 13.6" M4 10C/1G 16GB 512GB Gar.: 1 év APPLE világgar
- Apple iPhone 14 Pro 512GB,Újszerű,Dobozával,12 hónap garanciával
- Beszámítás! Samsung Odyssey G5 32 QHD 144Hz VA 1ms monitor monitor garanciával hibátlan működéssel
Állásajánlatok
Cég: ATW Internet Kft.
Város: Budapest
Cég: Laptopműhely Bt.
Város: Budapest


