将IF语句放在循环中?

时间:2016-02-24 12:48:58

标签: c++ if-statement

我正在尝试编写一个接受偶数输入的程序,直到用户输入一个奇数,然后它停止并添加所有偶数输入。

我遇到的问题是我打算使用if语句来确定数字是否均匀但是一旦程序运行我需要它再次运行以便用户可以继续输入数字直到输入奇数。

这是我到目前为止所拥有的:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    int value;
    cout << "Please enter a possitive number: ";
    cin >> value;
    if (value%2 == 0) { // divides the value entered by 2 too determine whether or not its even.
        cout << "Please enter another even number: ";
    }
    else
    {

    }

    cin.get();
    return 0;
}

4 个答案:

答案 0 :(得分:3)

如果值是偶数,您需要一个循环才能再次请求输入。

仅当while为偶数时,才会输入value循环。然后循环直到value输入均匀。

然后你需要另一个代表总和的int,并在每次进入循环时将值加到总和上(每次你有一个偶数)

请注意,使用此解决方案,您不需要if声明。条件位于while循环

#include <iostream>

using namespace std;

int main()
{
    int value;
    int sum=0;
    cout << "Please enter a possitive number: "<<endl;
    cin >> value;
    while (cin && value%2 ==0){
        sum+=value;
        cout << "Please enter another even number: "<<endl;;
        cin >> value;
    }

    cout<<"Sum of even number: "<<sum<<endl;
    return 0;
}

答案 1 :(得分:1)

做吧。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int value;
    vector<int> inputs;
    cout << "Please enter a possitive number: ";
    while (cin >> value) { // loop while successfully read an integer
        if (value%2 == 0) { // divides the value entered by 2 to determine whether or not its even.
            inputs.push_back(value);
            cout << "Please enter another even number: ";
        }
        else
        {
            break; // get out of this loop
        }

    }

    // add all the even inputs
    int sum = 0;
    for (vector<int>::iterator it = inputs.begin(); it != inputs.end(); it++) {
        sum += *it;
    }
    cout << sum << endl;

    return 0;
}

或者,您可以在循环内添加。

#include <iostream>

using namespace std;

int main()
{
    int value;
    int sum = 0;
    cout << "Please enter a possitive number: ";
    while (cin >> value) { // loop while successfully read an integer
        if (value%2 == 0) { // divides the value entered by 2 to determine whether or not its even.
            sum += value;
            cout << "Please enter another even number: ";
        }
        else
        {
            break; // get out of this loop
        }

    }

    cout << sum << endl;

    return 0;
}

答案 2 :(得分:0)

这样的事情?

int value;
bool run = true;
cout << "Please enter a possitive number: ";
while(run)
{

     cin >> value;
     if (value%2 == 0) { // divides the value entered by 2 too determine whether or not its even.
        cout << "Please enter another even number: ";
    }
    else
    {
       run=false
    }
}

sudo代码的模型类似于你的循环,所以不是最好的方法,也很可能是语法错误,但应该给你一个想法

答案 3 :(得分:0)

另一种检查是偶数还是奇数的方法是检查最低位:

#include <vector> #include <iostream> #include <algorithm> int main() { int enteredValue = 0; std::vector<int> result; do { std::cout<<"Please enter even number : "; std::cin>>enteredValue; result.push_back(enteredValue); std::cout<<'\n'; }while(!(enteredValue & 0x01)); //check the lowest bit std::for_each(result.begin(), result.end(), [](int x){ std::cout<<x<<' '; }); return 1; }