【问题标题】:I can't get this (simple) loop to work properly我无法让这个(简单)循环正常工作
【发布时间】:2014-02-27 21:18:32
【问题描述】:

我现在正在上编程课,并被要求创建一个程序来计算用户输入的多个数字的总和 - 然后计算总和的第 n 个根。如果他们输入的数字小于0,循环应该丢弃小于0的数字,然后再询问。

不幸的是,无论我输入什么数字,它都会显示“值需要大于零!”我尝试将fflush(stdin); 语句放入循环中,但这似乎没有任何作用。

这是我的代码。我非常感谢任何和所有的帮助。

#include "stdafx.h"
#include <stdio.h>
#include <math.h>

int main() {

int mTotalNums, mNth; //amount of numbers in set
float mProd = 1, x, mNroot;

printf("How many numbers are in the set?\n");
scanf("%i", &mTotalNums);

mNth = mTotalNums; //set the value of mTotalNums equal to mNth becuase we'll lose the original value of mTotalNums after the loop

while (mTotalNums > 0) {
    printf("Input number: ");
    scanf("%lf", &x);
    if (x > 0) {
        mProd *= x;
    } else
        printf("\nValue needs to be greater than zero!\n");
}

mNroot = pow(mProd, (1 / mNth));

printf("\nThe nth root of the product of %i terms is: %.2f\n", mNth, mNroot);

return 0;
}

【问题讨论】:

  • 你试过在'if'语句上设置断点来看看'x'的值是多少?
  • 要么像你一样将你的 x 定义为浮点数,并在 scanf 中使用“%f”,或者将其定义为双精度数,并使用“%lf”。现在你使用了错误的格式。

标签: c visual-studio while-loop


【解决方案1】:

"%lf"double 的scanf 格式,但x 被声明为float。 要扫描浮点数,您必须使用%f 格式。

还要注意mTotalNums 在循环中不会递减,因此它永远不会 终止。

【讨论】:

    【解决方案2】:

    阅读scanf(3)的文档。由于x 被声明为float,因此使用%f 作为scanf 格式控制字符串。另外,请考虑scanf 的结果(如果成功读取一项则为1)。

    您应该在编译器中启用所有警告和调试信息,然后学习如何使用调试器(尤其是逐步运行程序、显示局部变量等......)。

    (在 Linux 上,如果使用 gcc -Wall -g 编译,您会收到一个有用的警告,而 gdb 调试器会很有帮助...)

    【讨论】:

      【解决方案3】:

      尝试对您的程序进行这些修改(添加 cmets 并进行更改)

      #include "stdafx.h"
      #include <stdio.h>
      #include <math.h>
      
      int main() {
      
          //amount of numbers in set
          int mTotalNums, mNth; 
          // Change to double for added precision
          double mProd = 1.0, x, mNroot;
      
          printf("How many numbers are in the set?\n");
          scanf("%i", &mTotalNums);
      
          // Set the value of mTotalNums equal to mNth becuase
          // we'll lose the original value of mTotalNums after the loop
          mNth = mTotalNums; 
      
          // Don't forget to decrement the loop counter
          while (mTotalNums-- > 0) {
              printf("Input number: ");
              scanf("%lf", &x);
              if (x > 0) {
                  mProd *= x;
              } else {
                  printf("\nValue needs to be greater than zero!\n");
              }
      
          }
      
          // Change to 1.0 to force compiler to treat as a double
          mNroot = pow(mProd, (1.0 / mNth));
      
          printf("\nThe nth root of the product of %i terms is: %.2f\n", mNth, mNroot);
      
          return 0;
      }
      

      您提到“计算和的第 n 个根”,但您的循环显然是在计算累积乘积。要更改它以计算总和,请尝试以下添加:

      // Declare a sum variable
      double sum =  0;
      // Sum inside your while loop
      sum += x;
      // Calculate the nth root of the sum instead
      mNroot = pow(sum, (1.0 / mNth));
      

      【讨论】:

      • 我不是说“和的第 n 个根”,我的意思是“乘积的第 n 个根”。那是我的错。但是,我很感激你重写我的程序。这很有帮助!
      【解决方案4】:

      添加 printf 命令以查看变量包含的内容,然后再在逻辑语句中检查它们。

      您还需要做一些事情来为您的 while 循环增加/减少您的变量...目前没有任何改变 mTotalNums,所以这将是一个无限循环。

        while (mTotalNums > 0) {
            printf("Input number: ");
            scanf("%lf", &x);
            printf("x=%d", x);
            if (x > 0) {
                mProd *= x;
            } else
                printf("\nValue needs to be greater than zero!\n");
            mTotalNums--;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 2012-05-19
        • 1970-01-01
        • 2019-01-01
        相关资源
        最近更新 更多