方法完全忽略for循环:

时间:2017-09-21 05:33:25

标签: c# arrays for-loop multidimensional-array

运行我的程序后,我注意到我的数组的所有值都等于1,运行调试器之后我注意到我的for循环应该已经填充了数组,但是我绝对不知道为什么。有人可以告诉我我做错了什么吗?

该计划到达这一行:

for (a = a; a >= 1000; a++) // Adds all prime numbers 1 through 1000 to primeA[];

然后跳转到:

for (int k = 0; k < primeA.Length; k++)

完整代码:

class calcPrime
{
    /* @Method: isPrimeSET
     * @param: int a, int b
     * @return: true / false
     * @ Builds a list of primes and sorts them into a multi dimensional array. 
     */
    public int[,] isPrimeSet(int a, int b)
    {
        int[,] primeNumArray = new int[999, 999]; // Prime Multi Array 1000 slots in each array
        int[] primeA = new int[999]; // All prime numbers a 1 to 1000
        int[] primeB = new int[999]; // All prime numbers b 1 to 1000

        ArrayList dumpArrayA = new ArrayList(); // None prime numbers
        ArrayList dumpArrayB = new ArrayList(); // None prime numbers

        for (a = a; a >= 1000; a++) // Adds all prime numbers 1 through 1000 to primA[];
        {
            if (isPrime(a) == true)
            {
                int i = 0; // Index of primeA
                primeA[i] = a; // Add primes to a
                i++;
            }

            else // Stores none prime numbers
            {
                int m = 0;
                dumpArrayA.Add(m);
                m++;
            }

            for (b = b; b >= 1000; b++) // Adds all prime number 1 through 1000 to primeB[];
            {
                if (isPrime(b) == true)
                {
                    int j = 0; // Index of primeB
                    primeB[j] = b; // Add primes to b
                    j++;
                }

                else
                {
                    int n = 0;
                    dumpArrayB.Add(n);
                    n++;

                }

            }
        }

        // Merge primesA[] and primesB[];
        for (int k = 0; k < primeA.Length; k++)
        {
            primeNumArray[k, 0] = primeA[k];
            primeNumArray[k, 1] = primeB[k];
        }

        // Print Contents of PrimeNumArray
        for (int l = 0; l >= primeNumArray.Length; l++) {
            Console.WriteLine(primeNumArray[l, l]);
        } 

        return primeNumArray;

    }

添加了其他语句和转储数组,徒劳地尝试使程序正常工作。另外我知道我在for循环中为变量分配了一个变量,我希望在调用方法时用户选择的位置设置值。

2 个答案:

答案 0 :(得分:1)

我认为数组的索引似乎不正确,

        if (isPrime(a) == true)
        {
            int i = 0; // Index of primeA
            primeA[i] = a; // Add primes to a
            i++;
        }

不仅仅改变primeA [0]值吗?而对于primeB数组

            int j = 0; // Index of primeB
            primeB[j] = b; // Add primes to b
            j++;

也许你应该定义i和j out of loop。

答案 1 :(得分:1)

那里有很多错误!!

我设法纠正了你的代码

这是您更正后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Prime_number
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] k = isPrimeSet(1, 1);
            Console.Read();
        }

        public static int[,] isPrimeSet(int a, int b)
        {
            int[,] primeNumArray; // Prime Multi Array 1000 slots in each array
            ArrayList primeA = new ArrayList(); // All prime numbers a 1 to 1000
            ArrayList primeB = new ArrayList(); // All prime numbers b 1 to 1000

            ArrayList dumpArrayA = new ArrayList(); // None prime numbers
            ArrayList dumpArrayB = new ArrayList(); // None prime numbers
            int i = 0;
            int m = 0;
            int j = 0; // Index of primeB
            int n = 0;

            for (a = 1; a <= 1000; a++) // Adds all prime numbers 1 through 1000 to primA[];
            {
                if (isPrime(a) == true)
                {
                    // Index of primeA
                    primeA.Add( a); // Add primes to a
                    i++;
                }
                else // Stores none prime numbers
                {
                    dumpArrayA.Add(m);
                    m++;
                }
            }

            for (b = 1; b <= 1000; b++) // Adds all prime number 1 through 1000 to primeB[];
            {
                if (isPrime(b) == true)
                {
                    primeB.Add(b); // Add primes to b
                    j++;
                }
                else
                {
                    dumpArrayB.Add(n);
                    n++;
                }

            }
            primeNumArray= new int[primeA.Count,2];
            // Merge primesA[] and primesB[];
            for (int k = 0; k < primeA.Count; k++)
            {
                primeNumArray[k, 0] = (int)primeA[k];
                primeNumArray[k, 1] = (int)primeB[k];
            }

            // Print Contents of PrimeNumArray
            for (int l = 0; l <= primeNumArray.GetLength(0)-1; l++)
            {
                Console.WriteLine(primeNumArray[l, 0] + "  " + primeNumArray[l, 1]);
            }

            return primeNumArray;

        }


        static bool isPrime(int num)
        {
            bool bPrime = true;
            int factor = num / 2;
            int i = 0;
            for (i = 2; i <= factor; i++)
            {
                if ((num % i) == 0)

                    bPrime = false;
            }
            return bPrime;
        }
    }
}

试一试:)!感谢