我想通过使用空格停止整个do-while循环并写出我的WriteLine,但是在按空格键时它什么也没做。我认为这与Thread.Sleep有关,也许它不允许用户在睡眠状态下输入。如果有人能启发我为什么这行不通,我将非常高兴
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace casinov2
{
class Program
{
static void Main(string[] args)
{
int x=3;
int y = 15;
int[] elemek = new int[7];
do{
while (!Console.KeyAvailable){
for (int i = 0; i < 5; i++){
Console.Clear();
Console.SetCursorPosition(x, y);
x++;
Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
System.Threading.Thread.Sleep(100);
if (i >= 4){
for (int j = 0; j < 5; j++){
Console.Clear();
Console.SetCursorPosition(x, y);
x--;
Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
System.Threading.Thread.Sleep(100);
}
i = -1;
}
}
}
} while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
Console.WriteLine("ready");
Console.ReadLine();
}
}
}
答案 0 :(得分:-1)
KeyAvailable
检查必须位于for
循环中:
static void Main(string[] args)
{
int x=3;
int y = 15;
int[] elemek = new int[7];
do{
for (int i = 0; i < 5; i++){
Console.Clear();
Console.SetCursorPosition(x, y);
x++;
Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
System.Threading.Thread.Sleep(100);
if (i >= 4){
for (int j = 0; j < 5; j++){
Console.Clear();
Console.SetCursorPosition(x, y);
x--;
Console.WriteLine("{0:██}{1:██}{2:██}", elemek[i], elemek[i + 1], elemek[i + 2]);
System.Threading.Thread.Sleep(100);
}
i = -1;
}
if (Console.KeyAvailable) break;
}
} while (Console.ReadKey(true).Key != ConsoleKey.Spacebar);
Console.WriteLine("ready");
Console.ReadLine();
}