在我的節目的開始計算時間我在做一個遊戲中,你必須破解像一個系統:類型號碼,你在屏幕上看到: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)構建您的程序。 –