Multiple conditions in for statement

前端 未结 4 1540
有刺的猬
有刺的猬 2021-01-28 02:20

I have this code in a function, but when it runs it does a long pause and then it says:

$floating point exception

I am assuming this is due to

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 02:54

    floating point exception - This means there's an arithmetic error.

    It looks like you're trying to stop the loop with j, but what you're actually doing is continuing the loop forever (because once you get j==1 the or condition is always true).
    What then happens is you loop i through all the Integer values back to 0 and get the exception.

    What I think you want to do is :

    for (i = 2; (i < number); i++)
    {
        if (number%i==0)
        {
            j = 1;
            break;
        }
    }
    

提交回复
热议问题