class Test2
{
public static void main(String[] args)
{
int count = 0;
for (int x=101;x<=200 ;x++ )
{
boolean b = true;
for (int y = 2;y < Math.sqrt(x);y++ )
{
if (x%y == 0)
{
b = false;
break;
}
}
if (b)
{
count++;
System.out.print(x+"\t");
}
}
System.out.print("101~200之间素数个数为:"+count);
}
}
class Test2
{
public static void main(String[] args)
{
int count = 0;
boolean b = true;
for (int x=101;x<=200 ;x++ )
{
for (int y = 2;y < Math.sqrt(x);y++ )
{
if (x%y == 0)
{
b = false;
break;
}
}
if (b)
{
count++;
System.out.print(x+"\t");
}
}
System.out.print("101~200之间素数个数为:"+count);
}
}
为什么结果不一样,程序只有一处区别:程序1的boolean b = true放在for循环上面,而程序2放在for循环里面。
|
|