gpt4 book ai didi

用于添加一周中几天的工作时间的 C++ 方程式

转载 作者:行者123 更新时间:2023-11-28 00:57:59 28 4
gpt4 key购买 nike

我想弄清楚如何将一周内的所有工作时间相加。 “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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com