0

我试图弄清楚如何将一周内的所有工作时间加起来。“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;
4

4 回答 4

1

尝试使用 for 循环:

int hours=0;
for(int i=0;i<5;i++){
    int hoursday;
    cout << "enter hours worked in day " << i+1 << ":" ;
    while(cin>>hoursday ){
        if(hoursday>0 && hoursday<10){
            hours+=hoursday;
            break;
        }
        else{
            continue;
        }
    }
}

cout <<"total hours in the week : "<<  hours << endl;
于 2012-04-11T23:04:58.467 回答
1

看来您是初学者,您的程序中有很多错误。但是,让我祝贺你以不同的方式思考。

您希望程序运行的方式至少需要一个最初设置为 0 的变量,然后将其加起来。我正在发布您的代码的改进版本。要查看它是否有效,请将其复制并粘贴到编译器中并查看结果。

你的代码应该是这样的。好吧,我的缩进很奇怪......

#include<iostream.h>
#include<conio.h>

void main()
{
    int hoursDay;
        hoursDay=0;
        int hoursday;
        for(int k=1;k<=5;k++)
             {
                 cout<<"Enter hours worked for day"<<k<<"\n";
                 cin>>hoursday;
                 if(hoursday>0&&hoursday<10)
                     {
                             hoursDay=hoursDay+hoursday;
                     }
                 else
                         {
                             cout<<"\ninvalid input";
                            }

                                 }
             int  hours = hoursDay;

             cout << endl;
             cout << endl;
             cout << "Total hours for week is " << hours;
    getch();
}      
于 2012-10-31T15:58:45.323 回答
1

每次输入时只需将 hoursDay 添加到 hourshours += hoursDay;

不要重复你的代码5次,使用循环

(虽然这看起来像是一个开始练习,所以你可能还没有涵盖循环)

于 2012-04-11T22:25:41.980 回答
0

The only problem is figuring out how to add them all when they are all represented by the same name.有几种方法可以解决这个问题,但如果不添加至少一个附加变量,它们都不可能。

一个简单的方法是添加一个新变量int totalHours(或浮点数或双精度,无论您使用什么)并将其预初始化为零。然后对于每个输入,设置totalHours += hoursDay;.

于 2012-04-11T22:25:38.513 回答