while循环,好像什么也没做?

发布于 2024-09-25 01:24:08 字数 972 浏览 3 评论 0原文

你好,我正在尝试在 C++ 中创建一个函数,它接受一个数字 i,并通过运行循环查找它的倍数来确定它是否是质数,然后通过系列测试。然而,似乎循环甚至没有被运行。我已经告诉它无论在循环中的哪个位置都输出,但我没有得到任何输出。这是代码:

#include <iostream>

using namespace std;

int main()
{
    int j =1;
    int z = 0;
    int i = 10;
    bool p = false;
    while (p = false){
        cout << "not starting ifs";
        z=i%j;
        if (z==0 && j>2){
        p=true;
        cout << "not prime" << endl << "loops to if";
        }
        else if (j==1){
            j++;
            cout <<"loops to else if 1";
            }
        else if ( i==2 || j==i ){
          p = true;
          cout << "prime" << endl << "loops to else if 2";
            }
            else {
            j++;
            cout << "loops to else";
                }
        }
            return 0;
}

我不在乎其背后的数学是否正确,我想自己弄清楚这一点以获取学习经验。但如果有人能用一个易于理解的解释帮助我解决这个问题,我将不胜感激!我对编程真的很陌生,所以我还不习惯行话。我期待您的建议!

Hi there i'm trying to make a function in C++ that takes a number, i, and decides if it is a prime number or not by running through a loop to find it's multiples, and then makes sure it isn't prime through a series of test. However, it seems the loop isn't even being run through. I've told it to output no matter where in the loop it is, but I get no outputs. Here is the code:

#include <iostream>

using namespace std;

int main()
{
    int j =1;
    int z = 0;
    int i = 10;
    bool p = false;
    while (p = false){
        cout << "not starting ifs";
        z=i%j;
        if (z==0 && j>2){
        p=true;
        cout << "not prime" << endl << "loops to if";
        }
        else if (j==1){
            j++;
            cout <<"loops to else if 1";
            }
        else if ( i==2 || j==i ){
          p = true;
          cout << "prime" << endl << "loops to else if 2";
            }
            else {
            j++;
            cout << "loops to else";
                }
        }
            return 0;
}

I don't care whether or not the math behind it is right, I want to figure that out myself for the learning experience. But if anyone could help me figure this out with a good easy to understand explanation I would appreciate it! I"m really new to programming, so I'm not used to jargon yet. I look forward to your advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鱼忆七猫命九 2024-10-02 01:24:08

您在执行此操作时使用 = 而不是 ==

while (p = false){

,将 false 分配给 p 并得到结果表达式的值为 false ,它在 while 循环中进行测试,导致退出循环。

You are using = instead of == in

while (p = false){

When you do that, you assign false to p and the result of the expression is false which gets tested in the while loop resulting in exiting of the loop.

青衫负雪 2024-10-02 01:24:08

更好的是,

while(!p)

毕竟这就是操作员的用途。

Better yet,

while(!p)

After all, that's what the operator is for.

清风疏影 2024-10-02 01:24:08
while (p = false) // obvious error ...

如果您没有找到它:它是您想要的 == 而不是 =

while (p = false) // obvious error ...

If you don't find it : it's == not = that you want.

东北女汉子 2024-10-02 01:24:08

将其更改为 while (false == p)

基本上,按顺序发生的情况如下:

  1. p 被分配为 false
  2. p 被转换为 bool

当您说 (p = false) 时, “p”被赋予值“false”。此后,while 循环的布尔条件测试“p”的值,该值现在为 false,并且永远不会进入循环。

change it to while (false == p)

Basically here is what happens in order:

  1. p is assigned false
  2. p is converted to bool

When you says (p = false), 'p' is assigned the value 'false'. After this, the boolean condition of the while loop tests for the value of 'p' which is now false, and the loop is never entered.

谜兔 2024-10-02 01:24:08

由于您的编译器似乎没有对此发出警告,因此您应该了解如何打开警告,或者学习使用“Yoda-conditions”的习惯:

while (false = p)

将导致编译错误。

Since your compiler doesn't seem to be warning you about it, you should either find out how to turn on the warning, or learn the habit of using "Yoda-conditions":

while (false = p)

will cause a compile error.

傻比既视感 2024-10-02 01:24:08

您需要将 while (p = false) 更改为 while (p == false)

说明:在 C/C++ 中,= 分配属性,而 == 比较值。您在这里所做的基本上是将 false 分配给 p。然后 while 循环检查表达式的值,即 p,它等于 false,因此不会运行。

You need to change the while (p = false) to while (p == false).

Explanation: In C/C++, = assigns a property, whereas == compares values. What you are basically doing here is assigning false to p. The while loop then checks the value of the expression, which is p, which equals false, so doesn't run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文