0

到目前为止,这是我的代码

#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

4 回答 4

1

You need to calculate the sum after getting the input!

But your whole calculation and loop usage is wrong. Here it's fixed:

#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;
}

Note the following:

  1. % returns modulus - num1 % 2 ==1 implies that num1 is odd. I took out your ternary ?: operators not because they're bad, but just because if is easier to read and in this case you're not doing anything if num1 is even.

  2. You were setting num2 at the start of your for loop. A while loop makes more sense in this situation, or a for loop without initialization for (;num1<=num2; num1+=2) {.

于 2012-10-19T22:56:30.803 回答
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
于 2012-10-19T23:03:30.010 回答
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)您的循环在您的coutandcin语句之上,因此它在用户输入任何数字之前运行。您需要将循环移动到用户已将数字提供给程序之后(下方)。

3)循环的逻辑可能不是你想要的。添加大括号后,这就是它正在做的事情(在“伪代码”中):

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++ 是一门具有挑战性的语言学习,但坚持下去!

于 2012-10-19T23:06:08.417 回答
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;
}
于 2012-10-19T23:40:47.667 回答