#include<stdio.h>
void main()
{ int i,m; scanf("%d",&m); for(i=2;i<=m-1;i++) if(m%i==0) break; printf("%d is not a prime number\n",m); else printf("%d is a prime number\n",m);
}
你的代码应该是
#include<stdio.h>
void main()
{ int i,m; scanf("%d",&m); for(i=2;i<=m-1;i++) if(m%i==0) { printf("%d is not a prime number\n",m);return; } printf("%d is a prime number\n",m);
}
#include<stdio.h> void main() { int i,m; scanf("%d",&m); for(i=2;i<=m-1;i++) {if(m%i==0) { printf("%d is not a prime number\n",m); break; } else printf("%d is a prime number\n",m);} }
问题很简单,你那个else没有和任何一个if配对,被这条语句( printf("%d is not a prime number\n",m);
)隔开了与if的联系,编译器不认识,
#include<stdio.h>
void main()
{ int i,m; scanf("%d",&m); for(i=2;i<=m-1;i++) if(m%i==0) { break; printf("%d is not a prime number\n",m); } else printf("%d is a prime number\n",m);
}