C#,试图从生成的数组中提取素数,我得到的只是 0

C#, Trying to pull prime numbers from generated array all I get is 0

The program will generate a sequence of randomly chosen numbers between 1 – 100. The sequence will consist of 20 elements. Your program should display the sequence once it is generated. Then, it will display all prime numbers within this sequence.

目前的代码:

class Program

    {
        int[] rnum = new int[100];
        int[] selecting = new int[20];
        static void Main(string[] args)
        {
            Program myProgram = new Program();
            myProgram.Numbers();
            myProgram.PrimeN();
            Console.ReadLine();
        }
        public void Numbers()
        {
            int[] rnum = new int[100];
            // Filling the array with values 1 to 100:
            for (int i = 0; i < rnum.Length; i++)
            {
                rnum[i] = i + 1;
            }
            // Shuffling the elements of the array:
            Random rnd = new Random();
            for (int i = 0; i < rnum.Length; i++)
            {
                int j = rnd.Next(i, rnum.Length);
                int temp = rnum[i];
                rnum[i] = rnum[j];
                rnum[j] = temp;
            }
            // Selecting and presenting 20 numbers:
            int[] selecting = new int[20];
            for (int i = 0; i < 20; i++)
                selecting[i] = rnum[i];
            selecting.ToList().ForEach(i => Console.Write(i.ToString() + ", "));
            Console.WriteLine();
        }
        public void PrimeN()
        {
            
            List<int> primeNumbers = new List<int>();           
            for (int i = 0; i < selecting.Length; i++)
            {
                if (IsPrime(selecting[i]))
                {
                 
                    primeNumbers.Add(selecting[i]);
                }
            }
            Console.Write("Non-Prime Numbers:");
            for (int i = 0; i < primeNumbers.Count; i++)
            {
                Console.Write(primeNumbers[i] + " ");
  
            }
         
        }
        bool IsPrime(int number)
        {
            for (int i = 2; i < Math.Sqrt(number); i++)
            {
                if (number % i == 0) return false;
            }
            return true;
        }

    }
}

我看到的第一个问题是,您正在创建局部变量,而不是使用您声明的实例成员,在您的方法运行后,没有任何改变......这一切都发生在方法的本地。

注释掉下面几行,你已经声明了它们

public static void Numbers(int size)
{
   //int[] rnum = new int[size];

   ...

   //int[] selecting = new int[20];

虽然都可以缩减为

private static readonly Random _r = new Random();

static void Main(string[] args)
{
   var numbers = Generate(100);
   var selected = numbers.Take(20).ToArray();
   Console.WriteLine("Selected : " + string.Join(", ", selected));
   Console.Write("Primes : " + string.Join(",", selected.Where(IsPrime)));
   Console.ReadLine();
}

public static int[] Generate(int size)
{
   var numbers = Enumerable.Range(1, 100).ToArray();
   // Shuffling the elements of the array:
   for (var i = 0; i < numbers.Length; i++)
   {
      var j = _r.Next(i, numbers.Length);
      var temp = numbers[i];
      numbers[i] = numbers[j];
      numbers[j] = temp;
   }

   return numbers;
}

private static bool IsPrime(int number)
{
   for (var i = 2; i < Math.Sqrt(number); i++)
      if (number % i == 0) return false;
   return true;
}

输出

Selected : 92, 79, 1, 44, 83, 48, 70, 49, 80, 19, 27, 65, 50, 40, 20, 10, 68, 25, 9, 28
Primes : 79,1,83,49,19,25,9

您在 Numbers() 函数中创建了名为 selecting 的局部变量。此局部变量的范围在 Numbers() 函数内。

当您尝试读取 PrimeN() 中的相同变量时,它指的是全局定义的 selecting 变量。

由于全局 selecting 变量中没有存储任何内容,因此 primeNumbers 数组中的结果为 0。

为了解决这个问题,不要在 Numbers() 函数中创建名为 selecting 的局部变量,使用全局定义的变量,即 selecting 定义在 class 级别 [=21] =]

...

public void Numbers()
{
    ...
    // Selecting and presenting 20 numbers:
    //COMMENT BELOW LINE
    //int[] selecting = new int[20];
    
    for (int i = 0; i < 20; i++)
        selecting[i] = rnum[i];   //This will refer member which is defined globally
    selecting.ToList().ForEach(i => Console.Write(i.ToString() + ", "));
    Console.WriteLine();
}