我正在尝试使用标准C-Free 5.0创建一个秒表程序。以下是目前的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
char button;
int minutes=0, seconds=0, millisec=0;
int main(void)
{
while(1)
{
reset:
button = '\0';
int minutes=0, seconds=0, millisec=0;
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 'a')
{
while(1)
{
cont:
button = '\0';
Sleep(10);
millisec++;
if(millisec == 100)
{
millisec = 0;
seconds++;
if(seconds == 60)
{
seconds = 0;
minutes++;
}
}
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 's')
{
while(1)
{
button = '\0';
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 'a')
{
goto cont;
}
if(button == 'd')
{
goto reset;
}
}
}
}
}
}
}
我想通过按下按钮'a'来启动秒表,但它不起作用。使用scanf()会暂停整个程序。有没有一种方法可以检测到按钮被按下并继续秒表程序?我的意思是在不暂停程序的情况下,特别是按下's'停止和再次按下'a'继续,并始终显示计时器。