Console.WriteLine("Guess the number");
Console.Read();
int Guess = (Console.Read());
Random random = new Random();
int rnd = random.Next(0, 10);
if (Guess == rnd)
{
Console.WriteLine("Correct");
Console.ReadLine();
}
else
{
Console.WriteLine("incorrect");
Console.ReadLine();
我尝试制作需要猜数字的游戏,但是如果我按Enter键,控制台将关闭
这里有一些问题:
Enter
,但是您要扔掉第一次按键(不将Console.Read
的值分配给任何东西),然后捕获Enter
按下Guess
。int
:int Guess = Console.Read();
,但是该值是条目的[[ascii值],而不是条目的值。他们按的数字。并且,由于我们正在捕获Enter
键(它是\r\n
的组合),因此该值为13
(第一个字符的值)。\n
字符,因此在我们输出结果并执行Console.ReadLine()
之后,它将读取\n
并立即返回