嘿所以我正在编制一个程序来计算到目前为止我的程序的能源成本,但我希望所有三个设备的瓦特小时数不仅仅是一个我怎么做?:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watt_hours = watts * hours_per_day * 30;
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
答案 0 :(得分:2)
您必须为每个设备使用独立的watts
变量和hours_per_day
。
考虑使用循环来实现这一点,它会使代码更清晰,尺寸更小;
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
static const double dollars_per_wh = 0.00008;
int main(int argc, char ** argv)
{
int num_apps = 3;
std::vector<std::string> v_apps;
v_apps.push_back("Air conditioner");
v_apps.push_back("Television");
v_apps.push_back("Washer");
double watt, hours, watt_hours = 0;
for(register int i = 0; i < num_apps; ++i)
{
cout << "How many watts for the " << v_apps[i].c_str() << "? ";
cin >> watt;
cout << "How many hours/day do you run the " << v_apps[i].c_str() << "? ";
cin >> hours;
watt_hours = watt_hours + ((watt * hours) * 30);
}
cout << "You have used " << watt_hours << " Watt-Hours of electricity." << endl;
return 0;
}
答案 1 :(得分:0)
我猜你可以用三重变量做以下事情:
#include <iostream>
using namespace std;
int main ()
{
double watts_ac, watts_tv, watts_wa;
double hours_per_day_ac, hours_per_day_tv, hours_per_day_wa;
double watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts_ac;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day_ac;
cout << "How many Watts for the Television? " ;
cin >> watts_tv;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day_tv;
cout << "How many Watts for the Washer? " ;
cin >> watts_wa;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day_wa;
watt_hours = (watts_ac * hours_per_day_ac + watts_tv * hours_per_day_tv + watts_wa * hours_per_day_wa) * 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}
或只汇总数据:
#include <iostream>
using namespace std;
int main ()
{
double watts, hours_per_day_ac, watt_hours, dollars_per_wh;
dollars_per_wh= .00008;
cout << " How many Watts for the Air conditioner? ";
cin >> watts;
cout << " How many hours/day do you run the Air Conditioner? ";
cin >> hours_per_day;
watts_hours = watts*hours_per_day;
cout << "How many Watts for the Television? " ;
cin >> watts;
cout << "How many hours/day do you run the Television? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
cout << "How many Watts for the Washer? " ;
cin >> watts;
cout << "How many hours/day do you run the Washer? " ;
cin >> hours_per_day;
watts_hours += watts*hours_per_day;
watt_hours *= 30; //I'm not sure what this 30 factor is due to though.
cout << "You have used " << watt_hours << " Watts-Hours of electricity." << endl;
}