为什么不对呢??
using System;
namespace Demo
{
public class sushu
{
public sushu()
{
while(1==1)
{
Console.WriteLine("请输入一个正整数");
string x=Console.ReadLine();
int a=int.Parse(x);
if(x=="e"||x=="E")
{
break;
}
else
{
bool yes=false;
yes=false;
for(int i=2;i<Math.Sqrt(a);i++)
{
if(a%i==0)
{
yes=true;
}
else
{
yes=false;
}
}
if(yes)
{
Console.WriteLine(x+"不是素数");
}
else
{
Console.WriteLine(x+"是素数");
}
}
}
}
}
}

解决方案 »

  1.   

    这段改成这样:
                        for (int i = 2; i <= Math.Sqrt(a); i++)
                        {
                            if (a % i == 0)
                            {
                                yes = true;
                                break;
                            }
                            else
                            {
                                yes = false;
                            }
                        }
      

  2.   

    for (int i = 2; i <= Math.Sqrt(a); i++)
                        {
                            if (a % i == 0)
                            {
                                yes = true;
                                break;
                            }
                            else
                            {
                                yes = false;
                            }
                        }
      

  3.   

    static void Main(string[] args)
    {
    while(1==1)
    {
    Console.WriteLine("请输入一个正整数:");
    string x=Console.ReadLine();
    if(x=="e"||x=="E")
    {
    break;
    }
    int a=int.Parse(x);

    bool yes=false;
    for(int i=2;i<=Math.Sqrt(a);i++)
    {
    if(a%i==0)
    {
    yes=true;
    Console.WriteLine(x+"不是素数");
    break;
    }
    }
    if(yes == false)
    Console.WriteLine(x+"是素数");
    }
    }
    这样可以,你写的逻辑有点问题,注意我是放在Main中的