作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想弄清楚如何将一周内的所有工作时间相加。 “hours”代表一周工作的小时数,“hoursDay”代表一天的工作小时数。唯一的问题是弄清楚如何在它们都由相同的名称表示时将它们全部添加。下面是我的代码:(谢谢)
cout << "Enter hours worked for day 1: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 1: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 2: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 2: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 3: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 3: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 4: ";
cin >> hoursDay;
cout << endl;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 4: ";
cin >> hoursDay;
}
cin.ignore (1);
cout << "Enter hours worked for day 5: ";
cin >> hoursDay;
while (hoursDay < 0 || hoursDay > 10)
{
cout << "Invalid number of hours - must be between 0 and 10.";
cout << endl;
cout << "Enter hours worked for day 5: ";
cin >> hoursDay;
}
cin.ignore (1);
hours = hoursDay;
cout << endl;
cout << endl;
cout << "Total hours for week is " << hours;
最佳答案
只需在每次输入时将 hoursDay 添加到 hours hours += hoursDay;
并且不要重复你的代码 5 次,使用循环
(尽管这看起来像是一个开始练习,所以您可能还没有涵盖循环)
关于用于添加一周中几天的工作时间的 C++ 方程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114824/
我是一名优秀的程序员,十分优秀!