代码如下 复制代码
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数:");//默认只许输入“q”或者输入大于1的整数
string numberString = Console.ReadLine();
while (numberString != "q")
{
int numberInt = Convert.ToInt32(numberString );
bool result = PanDuan (numberInt );
if (result == true)
Console.WriteLine("{0}是质数", numberInt);
else
Console.WriteLine("{0}不是质数",numberInt );
Console.WriteLine("请输入一个数:");
numberString = Console.ReadLine();
}
Console.WriteLine("操作结束!!!");
Console.ReadKey();
}
public static bool PanDuan(int number)
{
bool result=true;
for (int i = 2; i < number; i++)
{
if (number == 2)
{
result = true;
break;
}
else if (number % i == 0)
{
result = false;
break;//break好像可以用,但是总是提示“检测到无法访问的代码”,这里一定不严谨
}
else
{
result = true;
break;
}
}
return result;
}
} |