| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 874 人关注过本帖
标题:倒计时
只看楼主 加入收藏
xiaxin
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-10-21
收藏
 问题点数:0 回复次数:5 
倒计时


#include<iostream>
#include<cstdlib>
#include<Windows.h>
using namespace std;
class Timer
{
private:
int day,hour,minute,second;
public:
Timer(int aDay,int aHour,int aMinute,int aSecond)//系统自动调用
{
day=aDay;//初始化的特殊性
hour=aHour;
minute=aMinute;
second=aSecond;
}

void coutDown();
void printTime();


};
int main()
{
Timer opening(1,0,0,2);
while(1)
{ system("cls");//数字被抹掉
opening.printTime();
Sleep(1000);//显示的数字停留1秒钟
system("cls");
opening.coutDown();
opening.printTime();
}
system("pause");
return 0;
}
void Timer::coutDown()
{
second--;

if(second<0)
{
minute--;
if(minute<0)
{ hour--;
minute=59;
if(hour<0)
{
day--;

hour=23;}}
second=60;
second--;

}


}
void Timer::printTime()
{

cout<<"距离奥运会还有:"<<day<<"天"<<endl;
cout<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}
这个程序有个问题,天数会出现负数,
请问怎样才能使其天数为负时跳出循环呢?

搜索更多相关主题的帖子: int 倒计时 include minute Timer 
2007-11-10 23:18
拉风
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-11-7
收藏
得分:0 

我把你的改了下,这样就行了
#include<iostream>
#include<cstdlib>
#include<Windows.h>
using namespace std;
class Timer
{
private:
int day,hour,minute,second;
public:
Timer(int aDay,int aHour,int aMinute,int aSecond)//系统自动调用
{
day=aDay;//初始化的特殊性
hour=aHour;
minute=aMinute;
second=aSecond;
}

void coutDown();
void printTime();
bool cut();


};
bool Timer::cut(){
if(day==0&&hour==0&&minute==0&&second==0)
return true;
else
return false;
}
int main()
{
Timer opening(1,0,0,2);
for(;;)
{ system("cls");//数字被抹掉
opening.printTime();
Sleep(1000);//显示的数字停留1秒钟
system("cls");
opening.coutDown();
opening.printTime();
if(opening.cut())break;
}
system("pause");
return 0;
}
void Timer::coutDown()
{
second--;

if(second<0)
{
minute--;
if(minute<0)
{ hour--;
minute=59;
if(hour<0)
{
day--;

hour=23;}}
second=60;
second--;

}


}
void Timer::printTime()
{

cout<<"距离奥运会还有:"<<day<<"天"<<endl;
cout<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}


一切都是从菜鸟开始的
2007-11-10 23:33
拉风
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-11-7
收藏
得分:0 

只是多了个中断函数而已,自己想想也能想出来的啊,不知道还有没有更好的方法


一切都是从菜鸟开始的
2007-11-10 23:36
xiaxin
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2007-10-21
收藏
得分:0 

谢谢,看来我还要多练练了

2007-11-10 23:48
liyingwei
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-10-23
收藏
得分:0 
怎么运行不了啊!编译时有3个错误!
2007-11-13 23:54
拉风
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2007-11-7
收藏
得分:0 

晕啊,楼上的你用什么编译器啊,我用VC6.0运行过了啊


一切都是从菜鸟开始的
2007-11-15 22:30
快速回复:倒计时
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021077 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved