在我的节目的开始计算时间我在做一个游戏中,你必须破解像一个系统:类型号码,你在屏幕上看到:12345在10秒内。我想知道如何在10秒过后警告玩家,如在屏幕上打印“太慢!!!!!!”。我尝试了sleep()
函数,但它停止了程序,而sleep()
函数正在运行!如何开始使用C
规则: 当你启动的程序,出现在屏幕上:
Enter code: Hack 1.
如果你输入1,然后输入您似乎有覆盖一个随机数。如果你没有出现:
Hacking failed!!!!!!!!.
你多慢显得过于它:
Too slow!!!!!!!
但那些事儿 “太慢了!!!!!!”只发生在节目结束时!
这里是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t start, end;
double need;
int i;
double number;
int t = 15;
z:
printf("Enter the code: Hack 1 :");
scanf("%d", &i);
if(i == 123456789)
{
printf("Enter the room.");
}
if(i == 1)
{
printf("You've got %d seconds. Press 1 to start hacking the system:", t);
scanf("%d", &i);
if(i == 1)
{
//Appears a random number and time starts counting
time (&start);
srand(time(NULL));
double rn = (rand() % 1000000000000000000);
printf("%f type here: ", rn);
scanf("%lf", &number);
if(number == rn)
{
//Time ends
time (&end);
//Calculate elapsed time
need = difftime (end, start);
//If you're too late
if(need > t)
{
printf("Too late!!!!!!!!!!!");
}
else
{
//If you success
printf("System hacked. Enter the room. ");
t--;
goto z;
}
}
else
{
//If you fail
printf("Hacking failed!!!!!!!!!!");
}
}
}
}
你应该阅读有关'for'循环和'while'循环。 – user3386109
您的程序执行必须在等待用户输入时阻止。如果你想打印“太迟了” 10秒后,同时等待有人进入输入你将不得不使用另一个线程 – mathematician1975
一个天真的答案可能是设置一个报警,但是这是很难得到正确。您需要深入了解事件驱动编程的深层复杂领域,并且可能围绕中心事件循环(如epoll和timerfds)构建您的程序。 –