using System
using System.Collections.Generic;
using System.Text;namespace number{
class Program
{
static void Main(string[] args)
{
bool result = true;
Console.WriteLine("{0}这个数是素数", 2);
for (int count = 3; count <= 100; count++)
{
for (int j = 2; j <= (int)(Math.Sqrt((double)j)); j++)
{
if (count % j == 0) result = false;
}
if (result) Console.WriteLine("{0}这个数是素数", count);
else Console.WriteLine("{0}这个数不是素数", count);
}
}}}
各位大虾请一下我这个程序错在哪里?我自己搞了半个小时了,还是发现不了错.
另外如果各位大虾有相关的源代码,敬请给小弟发一份,我想对照你们的代码来纠正自己的错误!
using System.Collections.Generic;
using System.Text;namespace number{
class Program
{
static void Main(string[] args)
{
bool result = true;
Console.WriteLine("{0}这个数是素数", 2);
for (int count = 3; count <= 100; count++)
{
for (int j = 2; j <= (int)(Math.Sqrt((double)j)); j++)
{
if (count % j == 0) result = false;
}
if (result) Console.WriteLine("{0}这个数是素数", count);
else Console.WriteLine("{0}这个数不是素数", count);
}
}}}
各位大虾请一下我这个程序错在哪里?我自己搞了半个小时了,还是发现不了错.
另外如果各位大虾有相关的源代码,敬请给小弟发一份,我想对照你们的代码来纠正自己的错误!
for (int count = 3; count <= 100; count++)
{
result = true;//Reset signal here
for (int j = 2; j <= (int)(Math.Sqrt((double)j)); j++)
{
if (count % j == 0)
{ result = false;
break;
}}
if (result) Console.WriteLine("{0}这个数是素数", count);
else Console.WriteLine("{0}这个数不是素数", count);
}
}}}
const int COUNT = 100;
int i = 2;
int count = 0; Console.WriteLine("前{0}个质数为:", COUNT);do
{
for (int j = 2; j < Math.Sqrt(i); j++)
{
if(i % j == 0)
{
flag = false; //如果不为质数
break;
}
}
if(flag == true)
{
count ++; //统计质数的个数
Console.Write(i + " ");
}
flag = true; //重置flag
i++;
}while(count < COUNT);
for (int count = 3; count <= 100; count++)
{
for (int j = 2; j <= (int)(Math.Sqrt((double)j)); j++)
{
if (count % j == 0)
result = false;
//在这里没有退出循环,当你已经发现一个数字不是质数的时候,你已经没有必要继续算下去了,由于你的内层循环一直在做,所以事实上你的count % j中,count是外层循环传进来的数字,而j由于一直在增加(即使已经知道count不是质数,j++仍然在做),那么你得到的result其实是count % 最大的那个j,自然和你的原意不符了
}
我现在正debug一步一步的step看一看,能不能找到错误也再请各位大虾帮忙斧正.
using System;
using System.Collections.Generic;
using System.Text;namespace number
{
class Program
{
static void Main(string[] args)
{
bool result = true;
Console.WriteLine("{0}这个数是素数", 2);
for (int count = 3; count <= 100; count++)
{
for (int j = 2; j <= (int)(Math.Sqrt((double)j)); j++)
{
if (count % j == 0) result = false;
} if (result) Console.WriteLine("{0}这个数是素数", count);
else Console.WriteLine("{0}这个数不是素数", count);
result = true;
}
Console.WriteLine("press any key to continue");
Console.ReadLine(); }
}
}
{
public static void Main()
{
bool flag = true;
const int COUNT = 100;
int i = 2;
int count = 0; Console.WriteLine("前{0}个质数为:", COUNT); do
{
for (int j = 2; j < Math.Sqrt(i); j++)
{
if(i % j == 0)
{
flag = false; //如果不为质数
break;
}
}
if(flag == true)
{
count ++; //统计质数的个数
Console.Write(i + " ");
}
flag = true; //重置flag
i++;
}while(count < COUNT); }}
我个人觉得我那个没错,可能问题出强制类型转换上吧!再有您说 要加上break其实不必要,这对于程序的正确性没有影响.
using System;namespace number
{
class Program
{
static void Main(string[] args)
{
bool result = true;
Console.WriteLine("{0}这个数是素数", 2);
for (int count = 3; count <= 100; count++)
{
//你错在下面这行
for (int j = 2; j <= (int)(Math.Sqrt(count)); j++)
{
if (count % j == 0)
{
result = false;
//再加这句
break;
} } if (result)
Console.WriteLine("{0}这个数是素数", count);
else
Console.WriteLine("{0}这个数不是素数", count);
result = true;
}
Console.WriteLine("press any key to continue");
Console.ReadLine(); }
}
}