输出特定的数组值

时间:2014-02-14 14:42:24

标签: c# arrays

我正在研究一个非常具体的问题。此程序提示用户输入1-10000000之间的数字,然后提示另一个数字1-10000。第一个数字是多少个随机数,第二个数字是这些数字的最大值。我试图在用户提示上打印结果并打印,如果用户想要打印结果,结果是素数。第一个问题是我无法让程序响应用户按1或0来打印结果。第二个是我无法获得作为重新打印创建的素数的数量

namespace PRIMECON
{
    class PRIMECON
    {
        static void Main(string[] args)
        {   
            //creates stopwatch
            Stopwatch watch = new Stopwatch();

            //creates randoms
            Random rnd = new Random();

            //starts the stopwatch
            watch.Start();

            //gets number from 1 to 10 million
            Console.WriteLine("\nPlease enter a number between 1 and 10,000,000");

            bool checkn;
            int number; //input

            checkn = int.TryParse(Console.ReadLine(), out number);

            //gets number from 1 to 10 thousand
            Console.WriteLine("\nPlease enter a number between 1 and 10,000");

            bool checkr;
            int range; //range of values from 0 to r

            checkr = int.TryParse(Console.ReadLine(), out range);

            if (checkn == true && checkr == true && number > 0 && number <= 10000000 && range > 0 && range <= 10000)
            {
                //array
                int arraySize = 0;
                //int[] array = new int[range];

                for (int i = 0; i < number; i++)
                {
                    int randomNumber = rnd.Next(1, range);
                    bool primeNumber = PRIMELIB.PRIMELIB.IsItPrime(randomNumber); //calls library method

                    if (primeNumber == true)
                        arraySize++; //adds to the array for each prime number
                    //array[i] = randomNumber;
                    //prints numbers
                    Console.WriteLine(i.ToString() + " " + randomNumber.ToString() + " " + primeNumber.ToString()); // prints index, number, true/false
                }
                //int[] array = new int[arraysize];
                watch.Stop();
                Console.WriteLine("\nElapsed Time = " + watch.Elapsed.ToString());

                int exit = 1;

                do
                {
                    Console.WriteLine("would you like to print results? 0-no 1-yes");
                    Console.ReadLine();

                    int rangeHigh;
                    int rangeLow;
                    bool high = int.TryParse(Console.ReadLine(), out rangeHigh);
                    bool low = int.TryParse(Console.ReadLine(), out rangeLow);

                    if (high == true && low == true)
                    {
                        for (int i = rangeLow; i < rangeHigh; i++)
                        {
                            //Console.WriteLine( i + ". " + array[i]);
                        }
                    }
                }
                while (exit != 0);
            }
            else if (checkn == false || number > 10000000 || number < 0)
                Console.WriteLine("First number was not valid");
            else if (checkr == false || range > 10000000 || range < 0)
                Console.WriteLine("Range was not valid");
            else
                Console.WriteLine("entry not valid");

            Console.ReadKey();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

目前,我可以发布一个关于“无法让程序响应用户按1或0打印结果”的问题的答案。由于您正在读取do / while循环中的控制台行,因此可以使用int.Parse函数,然后将while循环的内部部分放在if语句中,这样只有在用户选择yes时才会执行。循环现在变为:

int exit = 1;

do
{
    Console.WriteLine("would you like to print results? 0-no 1-yes");
    exit = int.Parse(Console.ReadLine());

    if (1 == exit)
    {
        int rangeHigh;
        int rangeLow;
        bool high = int.TryParse(Console.ReadLine(), out rangeHigh);
        bool low = int.TryParse(Console.ReadLine(), out rangeLow);
        if (high == true && low == true)
        {
            for (int i = rangeLow; i < rangeHigh; i++)
            {
                //Console.WriteLine( i + ". " + array[i]);
            }
        }
    }
}
while (exit != 0);

对于您的库问题而无法获取已创建的素数数量,我唯一的建议是检查您的引用以查看是否包含它,以及您是否在代码顶部添加了使用它。我没有在c#中使用自定义库,但在谷歌搜索如何在c#中使用库将是你最好的选择。