在 C 中显示两个用户定义数字之间的质数



我收到错误:运行时需要左键作为赋值的左操作数

for(x=2;x<=n/2;x++)
{
     if ((n%x)=0)
     {
        y=1;
      }
 }

值运算符 (=( 和比较运算符 (==( 之间存在差异。在代码中,您正在比较,因此您应该使用"==">

for(x=2;x<=n/2;x++)
{
 if ((n%x)==0)
 {
    y=1;
 }
}

就像其他人说的那样,问题出在if声明的状况上。比方说n = 13.它进入for语句。所以第一步是这样的:

if(1 = 0)//13 % 2 = 1
    y = 1;

这就像说 5 = 9 或 100 = 12 或 5 = 0。这是不可能的。要检查n % x是否为 0,请使用 n % x == 0

http://www.includehelp.com/c-programming-questions/what-is-difference-between-assignment-and-equalto-operator.aspx

最新更新