修改代码,使得结果正确
#include <iostream.h>class Time
{
private:
int hours, minutes, seconds;
public:
void get_time()
{
cin>>hours>>minutes>>seconds;
}
void display_time()
{
cout<<hours<<':'<<minutes<<':'<<seconds<<endl;
}
void add_time(Time & t1, Time & t2)
{
hours=t1.hours+t2.hours;
minutes=t1.minutes+t2.minutes;
seconds=t1.seconds+t2.seconds;
if(seconds>=60)
{
seconds-=60;
minutes++;
}
if(minutes>=60)
{
minutes-=60;
hours++;
}
}
};
void main()
{
Time one, two, three;
cout<<"\nEnter the first time(hours minutes seconds):";
one.get_time();
cout<<"\nEnter the second time(hours minutes seconds):";
two.get_time();
three.add_time(one,two);
cout<<"the result is:"<<endl;
three.display_time();
} 代码中缺少了头文件的引用,应该加上`#include <iostream>`。
另外,在`display_time()`函数中,缺少了要输出的时间信息。应该在`cout`语句中添加具体的输出内容,例如:
cout << "Time: " << hours << ":" << minutes << ":" << seconds << endl;
这样就可以正确显示时间了。
下面是修改后的完整代码:
#include <iostream>
using namespace std;
class Time
{
private:
int hours, minutes, seconds;
public:
void get_time()
{
cin >> hours >> minutes >> seconds;
}
void display_time()
{
cout << "Time: " << hours << ":" << minutes << ":" << seconds << endl;
}
};
int main()
{
Time t;
t.get_time();
t.display_time();
return 0;
}
你可以运行这段代码并输入时间,然后会正确地显示出输入的时间。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 以下是按照要求修改后的程序:
#include <iostream>
class Time
{
private:
int hours, minutes, seconds;
public:
Time() : hours(0), minutes(0), seconds(0) {}
void get_time()
{
std::cin >> hours >> minutes >> seconds;
}
void display_time()
{
std::cout << hours << ':' << minutes << ':' << seconds << std::endl;
}
void add_time(Time & t1, Time & t2)
{
seconds = t1.seconds + t2.seconds;
minutes = t1.minutes + t2.minutes;
hours = t1.hours + t2.hours;
if(seconds >= 60)
{
minutes += seconds / 60;
seconds %= 60;
}
if(minutes >= 60)
{
hours += minutes / 60;
minutes %= 60;
}
}
};
int main()
{
Time one, two, three;
std::cout << "\n输入第一个时间(小时 分钟 秒): ";
one.get_time();
std::cout << "\n输入第二个时间(小时 分钟 秒): ";
two.get_time();
three.add_time(one, two);
std::cout << "结果是: ";
three.display_time();
return 0;
}
求最佳答案{:10_254:}{:10_254:}{:10_254:}
页:
[1]