用c ++创建倒数计时器 [英] Creating a countdown timer in c++

查看:124
本文介绍了用c ++创建倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中创建一个分秒倒计时器。所以,我尝试了一些东西并且它有效。但我不知道我的方法是否正确,我想确定我是否以正确的方式做到了。所以,请有人评论我的代码并建议我是否需要更改。

谢谢。



I wanted to create a minute and seconds countdown timer in c++. So, i tried something and it worked. But i dont know if my approach is correct and i want to be sure if i was doing it the right way. So, please can anyone comment on my code and suggest me if anything is required to be changed.
Thanks .

#include <iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;

int main()
{
	int m,s;
	int total;


	
	cout<<"Enter m and s"<<endl;
	cin>>m>>s;


	
		for(int sec = s; s>=0; s--)
		{
			
			cout<<"You have "<<"minutes"<<m<<"seconds"<<s<<endl;
			Sleep(200);
			
			if(s ==0)
			{
             total = m*60;

			 for(int min = total; total>=0; total--)
			 {
		 cout<<"You have the remaining seconds"<<total<<endl;
				 Sleep(200);
			 }



			}
		}

	



	 system("pause");


}

推荐答案

这有点奇怪 - 可能对用户来说非常烦人,但那是无论是在这里还是在那里。



看看它:分到秒的重点是什么?你再也不用sec ......

当m为10,s为30时会发生什么?

它开始告诉用户他有10分钟和30分钟, 10和29,... 10和1.

然后它发射到600秒,599秒,......

嗯 - 差不多,它不算数几秒钟 - 200不是秒,它是2/10秒。



我会采用不同的方式:

分钟和秒钟。

以秒为单位生成总计。

循环直到总数为零。

在循环中,转换为将当前总数分为几分钟和几秒并打印出来。



实际上,我要做的是获取当前日期和时间,添加偏移并将其设置为目标时间。然后我会读取循环中的当前时间并将其与目标进行比较 - 这允许应用程序外部的系统事件通过暂停应用程序来减缓进度。但对于初学者来说这可能有点先进了!
It's kinda odd - and probably very annoying for users, but that's neither here nor there.

Look at it: what is the point of assinging s to sec? You never use sec again...
And what happens when m is 10, and s is 30?
It starts off telling the user he has 10 mins and 30, 10 and 29, ...10 and 1.
Then it launches into 600 seconds, 599 seconds,...
Well - almost, it doesn't count in seconds at all - 200 is not a second, it's 2/10ths of a second.

I would do it differently:
Read in the minutes and seconds.
Generate a "total" in seconds.
Loop until the total is zero.
In the loop, convert the current total into minutes and seconds and print it.

In fact what I'd do is take the current date and time, add the offset and set that as the target time. I'd then read the current time in the loop and compare that to the target - this allows for system events outside the application which slow the progress by suspending the app. But that's probably a little advanced for a beginner!


这篇关于用c ++创建倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆