向各位请教一个简单的问题?
#include<iostream>
using namespace std;
int main()
{
int t=0,t1=0,t2=0,hour1,hour2,minute1,minute2;
cout<<"Please input the first time:";
cin>>hour1>>":">>minute1;
{
if((hour1<0 && hour1>24)||(minute1<0 && minute1>60))
cout<<"Please input the hour from 0 to 24 and the minute from 0 to 60"<<endl;
cout<<"Please input the second time:";
}
cin>>hour2>>":">>minute2;
{
if((hour2<0 && hour2>24)||(minute2<0 && minute2>60))
cout<<"Please input the hour from 0 to 24 and the minute from 0 to 60"<<endl;
}
t1=60*60*hour1+60*minute1;
t2=60*60*hour2+60*minute2;
if(t1>t2)
t=t1-t2;
else
t=t2-t1;
cout<<"The different time is: "<<t<<"S"<<endl;
return 0;
}
该程序实现的功能是:向程序输入两个时间,然后求出两个时间差。
[解决办法]
cin >>hour1 >>":" >>minute1;
cin >>hour2 >>":" >>minute2;
把这两句改一下,改成下面这样就行了:
cin >>hour1 >>minute1;
cin >>hour2 >>minute2;
[解决办法]
- C/C++ code
if((hour1 <0 && hour1 >24) ¦ ¦(minute1 <0 && minute1 >60))cout < <"Please input the hour from 0 to 24 and the minute from 0 to 60" < <endl;cout < <"Please input the second time:"; exit(1);//加个这个函数让程序结束}
[解决办法]
把程序改了一下, 你运行看看,你的那个条件式
if((hour1 <0 && hour1 >24) ¦ ¦(minute1 <0 && minute1 >60))
逻辑不对,应该是
if((hour2 <0 || hour2 > 23)||(minute2 < 0 || minute2 >59))
我运行了,好像没什么问题,你看看吧
- C/C++ code
#include <iostream>using namespace std;int main(){ int t=0,t1=0,t2=0,hour1,hour2,minute1,minute2; do{ cout<<"Please input the first time:"; cin>>hour1>>minute1; if((hour1 < 0 || hour1 > 23)||(minute1 < 0 || minute1 >59)) { cout<<"Please input the hour from 0 to 24 and the minute from 0 to 60"; cout<<"\n"<<endl; } } while((hour1 <0 || hour1 >24)||(minute1 <0 || minute1 >60)); do{ cout<<"Please input the second time:"; cin>>hour2>>minute2; if((hour2 <0 || hour2 > 23)||(minute2 < 0 || minute2 >59)) { cout<<"Please input the hour from 0 to 24 and the minute from 0 to 60"; cout<<"\n"<<endl; } } while((hour2 <0 || hour2 > 23)||(minute2 < 0 || minute2 >59)); t1=60*60*hour1+60*minute1; t2=60*60*hour2+60*minute2; if(t1 >= t2) t=t1-t2; else t=t2-t1; cout <<"The different time is: " <<t <<"S" <<endl; return 0;}