每秒清除一次终端,但留下几分钟



>我有一个秒和分钟计数器,与我的计时器非常相似。但是,我无法获得留在屏幕上的分钟数。

int main()
{
int spam = 0;
int minute = 0;
while (spam != -1)
{
spam++;
std::cout << spam << " seconds" << std::endl;
Sleep(200);
system("CLS");
//I still want the system to clear the seconds
if ((spam % 60) == 0)
{
minute++;
std::cout << minute << " minutes" << std::endl;
}
//but not the minutes
}
}

system("CLS")将清除屏幕,您执行while循环的每次迭代,而您每分钟左右只打印一次minute

您需要在每次迭代时打印分钟:

while (spam != -1)
{
spam++;
if (minute)
std::cout << minute << " minutes" << std::endl;
std::cout << spam << " seconds" << std::endl;
Sleep(200);
system("CLS");
if ((spam % 60) == 0)
{
minute++;
}
}

在这里,我假设您只想打印不为零的分钟,因此if (minute).

FWIW:你可能想在更新minute时将spam重置为0,但这取决于你在做什么。也许您只是希望显示总经过的数。

最新更新