C ++ - 我的循环不断加起来为0

时间:2012-10-19 22:51:56

标签: c++ loops for-loop

到目前为止这是我的代码

#include <iostream>
using namespace std;

int main ()
{
int num1 = 0;
int num2 = 0;
int sum = 0;


for(num2 = num1; num1 <= num2; num1 +=2) sum += num1;
    num1 = num1 / 2 == 0? num1 : num1 + 1;
    num2 = num2 / 2 == 0? num2 : num2 - 1;

cout << "Enter the First Number:" << endl;
cin >> num1;
cout << "Enter the Second Number:" << endl;
cin >> num2;
cout << "Total Sum: " << sum << endl;
  } //end for

但总和一直在加0:/

这就是问题所在。

创建一个程序,显示用户输入的两个数字之间的偶数之和。换句话说,如果用户输入偶数,则该数字应包含在总和中。例如,如果用户输入整数2和7,则总和为12(2 + 4 + 6)。如果用户输入整数2和8,则总和为20(2 + 4 + 6 + 8)。如果用户输入的第一个整数大于第二个整数,则显示错误消息。

4 个答案:

答案 0 :(得分:1)

您需要在获取输入后计算总和

但是你的整个计算和循环使用是错误的。这里修好了:

#include <iostream>
using namespace std;

int main ()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;

    cout << "Enter the First Number:" << endl;
    cin >> num1;
    cout << "Enter the Second Number:" << endl;
    cin >> num2;

    if (num1 % 2 == 1) num1 += 1;
    if (num2 % 2 == 1) num2 -= 1;

    while (num1 <= num2) {
        sum += num1;
        num1 += 2;
    }

  cout << "Total Sum: " << sum << endl;
}

请注意以下事项:

  1. %返回模数 - num1 % 2 ==1表示num1是奇数。我拿出你的三元?:运算符不是因为它们不好,而是因为if更容易阅读,在这种情况下,如果num1是偶数,你就不会做任何事情。< / p>

  2. 您在for循环开始时设置了num2while循环在这种情况下更有意义,或者for循环没有初始化for (;num1<=num2; num1+=2) {

答案 1 :(得分:1)

代码是按顺序执行的,for循环初始化会让你失去循环的边界,而不是考虑这段代码。

#include <iostream>
using namespace std;

int main ()
{
    int num1 = 0;
    int num2 = 0;
    int sum = 0;

    cout << "Enter the First Number:" << endl;
    cin >> num1;
    cout << "Enter the Second Number:" << endl;
    cin >> num2;

    if (num1 > num2) // swap the numbers and do not print error message
    {
        int temp = num1;
        num1 = num2;
        num2 = temp;
    }
    //make sure to start from even number
    num1 = num1 % 2 ? num1+1 : num1;

    for(; num1 <= num2; num1 +=2) 
        sum += num1;    
    cout << "Total Sum: " << sum << endl;
  } //en

答案 2 :(得分:1)

虽然家庭作业(我假设)应该由您解决,但这里有一些提示可以帮助您:

1)你的for循环需要围绕应该循环的代码的大括号:

for(num2 = num1; num1 <= num2; num1 +=2)
{
    sum += num1;
    num1 = num1 / 2 == 0? num1 : num1 + 1;
    num2 = num2 / 2 == 0? num2 : num2 - 1;
}

2)您的循环高于coutcin语句,因此它会在用户输入任何数字之前运行。您需要将循环移动到用户为程序提供的数字之后(下方)。

3) 循环的逻辑可能不是你想要的。一旦添加了大括号,这就是它正在做的事情(&#34;伪代码&#34;):

Let num2 equal num1 // Both are set to zero so this doesn't do anything
While num1 is less than or equal to num2:
{
    Add the current value of num1 to sum.
    if num1 /2 (ignoring remainder) is 0, then set num1 equal to itself. Otherwise, add 1 to it.
    // num1 already equals itself, so this doesn't do anything when num1 / 2 is zero.
    // 
    if num2 /2 (ignoring remainder) is 0, then set num1 equal to itself. Otherwise, subtract 1 from it.
    Add 2 to num1.
}

除非作业另有说明,否则最好不要使用三元(?和:)语法,因为当你刚刚开始编程时它至少令人困惑(至少,我这么想)。

C ++是一门具有挑战性的语言,但要坚持下去!

答案 3 :(得分:0)

1.)获取数字 2.)确定最大值和最小值 3.)之间的平衡

#include <iostream>
using namespace std;

void main()
{
   int num1 = 0;
   int num2 = 0;
   int sum = 0;
   int temp = 0;
   int i;

   //Get your input values
   cout << "Enter the First Number:" << endl;
   cin >> num1;
   cout << "Enter the Second Number:" << endl;
   cin >> num2;
   cout << endl;    

   //just to reorganize and make num1 the smallest of the two
   if ( num2 << num1 )
   {
       temp = num1;
       num1 = num2;
       num2 = temp;
   }    

   //loop through and add even values
   for(i = num1; i < num2; i++)
   {
       if(i%2 == 0)
       {
           sum = sum + i;
       }
   }

   cout << "Sum: " << sum << endl;
}