【问题标题】:C++ - my loop keeps on adding up to 0C++ - 我的循环一直加到 0
【发布时间】:2012-10-10 14:44:54
【问题描述】:

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

#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 )。如果用户输入的第一个整数大于第二个整数,则显示错误消息。

【问题讨论】:

  • 你不能指望用户在循环之后输入的值在循环中很重要。
  • 如果这是整个程序,那就不足为奇了。您计算 sum,它加到 0 并之后读入一些数字。
  • 我刚试过。还是一样的..

标签: c++ loops for-loop


【解决方案1】:

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

【讨论】:

  • 为了增加趣味性,您可以将代码放在一个循环中,这样它会在运行后重复取值,直到用户说停止。但我会让你自己弄清楚。祝你好运。
【解决方案2】:

虽然作业(我认为)应该由您完成,但这里有一些提示可以帮助您:

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

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】:

    你需要得到输入后计算总和!

    但是您的整个计算和循环使用是错误的。这里是固定的:

    #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 是偶数,你什么都不会做。

    2. 您在 for 循环的开头设置了 num2。在这种情况下,while 循环更有意义,或者没有初始化的for 循环for (;num1&lt;=num2; num1+=2) {

    【讨论】:

      【解决方案4】:

      代码是顺序执行的,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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-31
        • 2020-09-29
        • 1970-01-01
        • 2020-05-17
        • 2019-04-20
        • 2022-01-12
        • 1970-01-01
        • 2013-12-05
        相关资源
        最近更新 更多