每秒清除终端,但保留几分钟
Clear terminal every second but leave minutes
我有一个秒和分钟计数器,与我的计时器非常相似。但是,我无法获得在屏幕上停留的分钟数。
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
,但这取决于您在做什么。也许您只是想显示总共经过的 秒 数。
我有一个秒和分钟计数器,与我的计时器非常相似。但是,我无法获得在屏幕上停留的分钟数。
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
,但这取决于您在做什么。也许您只是想显示总共经过的 秒 数。