Ú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!
- World of Tanks - MMO
- Futás, futópályák
- Kuponkunyeráló
- Okosóra és okoskiegészítő topik
- PROHARDVER! feedback: bugok, problémák, ötletek
- A fociról könnyedén, egy baráti társaságban
- Milyen egeret válasszak?
- Ne már! Drágább lesz a GPU a memóriapánik miatt?
- Kerékpárosok, bringások ide!
- Redmi Note 14 Pro+ 5G - a tizenhármas átka
- További aktív témák...
- HIBÁTLAN iPhone 14 512GB Blue -1 ÉV GARANCIA - Kártyafüggetlen, MS3922, 100% Akkumulátor
- LG OLED & OLED evo Televíziók: BLACK NOVEMBER -30%
- BESZÁMÍTÁS! ASRock B450M R5 3600 16GB DDR4 256GB SSD 2TB HDD RTX 2060 Super 8GB RAMPAGE Shiva 450W
- Google Pixel 10 Pro 5G Porcelain AI-fotó varázs, Super Actua 120 Hz 128 GB
- HP 14 Elitebook 840 G7 FHD IPS i5-10210U 4.2Ghz 16GB 256GB SSD Intel UHD Graphics Win11 Pro Garancia
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest

