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);
      }
}}}
各位大虾请一下我这个程序错在哪里?我自己搞了半个小时了,还是发现不了错.
另外如果各位大虾有相关的源代码,敬请给小弟发一份,我想对照你们的代码来纠正自己的错误!

解决方案 »

  1.   

    Sample code as follows
    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);
          }
    }}}
      

  2.   

    //列出前100个质数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);
      

  3.   

    //LZ出错的原因 
              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,自然和你的原意不符了
                    }
      

  4.   

    我测试了一下各位大哥的说法,不过错了,你们都错了.
    我现在正debug一步一步的step看一看,能不能找到错误也再请各位大虾帮忙斧正.
      

  5.   

    另附我正测试的最后代码:
    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();        }
        }
    }
      

  6.   

    我这个调试过的,不可能错using System;public class Test
    {
    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); }}
      

  7.   

    我说我那个请您帮忙debug一下.
    我个人觉得我那个没错,可能问题出强制类型转换上吧!再有您说 要加上break其实不必要,这对于程序的正确性没有影响.
      

  8.   

    你的我调好了
    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(); }
    }
    }