【问题标题】:Exits while loop before condition is false?在条件为假之前退出while循环?
【发布时间】:2015-02-18 12:57:12
【问题描述】:

这是一个非常具体的问题,但我的程序似乎在条件为假之前退出了它的 while 循环。我在调试时添加了很多内存检查以确保安全,它在屏幕上显示计数器为 4,最后 SqRoot 为 6,这意味着它仍应循环通过 (TestNum=32)。我绝对知道它通过 counter

编辑:我改变了程序的整体逻辑,它现在可以工作了。谢谢!

#include <iostream>
#include <cmath>
using namespace std;

//Declare variables.
int TestNum, DivInt, SqRoot, PrintCounter(0), oppcounter;
float DivFloat, counter(2);

int main()
{
//Prompt user for input.
cout << "Input an positive integer to test its primality.";
cin >> TestNum;

//Check if input is positive.
while (TestNum < 0)
{
    cout << "Please input a *positive* integer.";
    cin >> TestNum;
}

//Square root.
SqRoot = sqrt(TestNum)+1;

//Loop to test if prime.
while (counter<=SqRoot)
{
    ++counter;
    DivFloat = TestNum/counter;
    DivInt = TestNum/counter;
    oppcounter = TestNum/counter;
    if (DivFloat-DivInt == 0)
    {
        ++PrintCounter;
        if (PrintCounter==1)
        {
        cout << "The integer " << TestNum << " is composite.\n " \
                << TestNum << " is divisible by\n";
        };

        cout << counter << "  " << oppcounter;

        cout << "counter* " << counter;
        cout << " TestNum " << TestNum;
        cout << " DivInt " << DivInt;
        cout << " SqRoot " << SqRoot;
        cout << " DivFloat " << DivFloat;
    }
}

if (counter<=SqRoot)
{
    cout << "The integer " << TestNum << " is prime.\n";
}

cout << "counter " << counter;
cout << " TestNum " << TestNum;
cout << " DivInt " << DivInt;
cout << " SqRoot " << SqRoot;
cout << " DivFloat " << DivFloat;

//End main.
return (0);
}

【问题讨论】:

  • 它在 if 中打印变量。它只是不再进入它,但循环继续。
  • 我最初是这么认为的,但我在 if 中将星号添加到“计数器”中,这是正在打印的最终检查。此外,它同时打印“整数 32 是复合数”和“整数 32 是素数”,因此它通过 counter 循环
  • 在我的最后工作完全正常(对于那个特定的测试用例)!你得到的确切错误是什么?我唯一看到的是您缺少}main()
  • @binaryBaBa 问题在于它同时打印“整数 32 是复合数”和“整数 32 是素数”。 if (counter&lt;=SqRoot) 是对添加的循环的检查。一旦它退出 while 循环,counter
  • 您的系统可能出现舍入错误。如果您要使用++ 作为循环计数器,那么变量应该是一个整数,而不是float。我建议将counter 更改为int,并将SqRoot 更改为double SqRoot = sqrt(TestNum)+0.5; - 这样您就可以避免任何接近整数的舍入问题。

标签: c++ while-loop


【解决方案1】:

我看到与您所描述的行为相反的行为,我明白为什么。您发布的代码可能与您正在执行的代码不同。

顺便说一句,我添加了一行

cout << endl;

行后

cout << " DivFloat " << DivFloat;

在几个地方使输出更具可读性。

当我输入 32 时,我看到以下输出:

整数 32 是合数。 32 可以被 4 8 计数器* 4 TestNum 32 DivInt 8 SqRoot 6 DivFloat 8 计数器 7 TestNum 32 DivInt 4 SqRoot 6 DivFloat 4.57143

当我输入17 时,我看到以下输出:

计数器 6 TestNum 17 DivInt 2 SqRoot 5 DivFloat 2.83333

原因:

  1. 当您检测到一个数字是合数时,您不会跳出while 循环。

  2. 因此,只有当 counter&lt;=SqRoot 的计算结果为 false 时,您才会跳出 @9​​87654325@ 循环。结果,在下面的代码中,

    if (counter<=SqRoot)
    {
       cout << "The integer " << TestNum << " is prime.\n";
    }
    

您永远不会执行 if 块中的行。

如果您在检测到复合时跳出while 循环并将最后一个if 块中的逻辑更改为:

if (counter > SqRoot)
{
   cout << "The integer " << TestNum << " is prime.\n";
}

【讨论】:

  • 好的,我发现我在最后的 if 中有错误的条件。但是,我的输出打印了“整数 32 是复合数”和“整数 32 是素数”。
  • 检测到复合时退出while 循环应该可以修复它。
  • 我也想列出所有因素,这意味着我必须一直循环,所以这就是为什么我不只是使用break;
  • 在这种情况下,您可以使用PrintCounter 的值。如果是0,则为素数,否则为合数。
【解决方案2】:

为什么对素数进行如此奇怪的检查?

for(int i = 2; i*i <= n; ++i)
{
     if (n % i == 0)
     {
          cout << "not prime";
          break;
      }
 }

【讨论】:

  • 以前从未做过。我试图自己弄清楚逻辑,结果走上了一条奇怪的道路。现在我不知道为什么它不起作用
猜你喜欢
  • 2022-01-22
  • 2019-10-26
  • 1970-01-01
  • 2011-02-08
  • 2018-02-08
  • 2017-02-25
  • 2022-09-23
  • 2011-07-22
  • 2023-01-12
相关资源
最近更新 更多