简体   繁体   English

输出特定的数组值

[英]outputing array values specific

i am working on a very specific problem. 我正在处理一个非常具体的问题。 this program prompts the user for a number between 1-10000000 and then another number 1-10000. 该程序提示用户输入1-10000000之间的数字,然后输入另一个1-10000。 the first number is how many random numbers and the second is what the max those numbers can be is. 第一个数字是多少个随机数,第二个是这些数字可以达到的最大值。 im trying to print the result on user prompt and print if user wants to print results and the result are number of primes. 我试图在用户提示符下打印结果,并在用户希望打印结果且结果为质数的情况下进行打印。 first problem is i cannot get the program to respond to the user pushing 1 or 0 to print results. 第一个问题是我无法让程序响应用户按1或0打印结果。 second is i cannot get the number of primes that were created to print as a resut 第二个是我无法获得要打印为素数的素数的数量

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();
        }
    }
}

At the moment I can post an answer about your "cannot get the program to respond to the user pushing 1 or 0 to print results" issue. 目前,我可以发布有关您的“无法使程序响应用户按1或0打印结果”问题的答案。 Since your are reading the console line within the do/while loop you can use the int.Parse function then put the inner part of the while loop in an if statement so it will only execute if the user picked yes. 由于您正在读取do / while循环中的控制台行,因此可以使用int.Parse函数,然后将while循环的内部放入if语句中,以便仅在用户选择yes时执行。 The loop will now become this: 现在,循环将变为:

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);

For your library issue with not being able to get the number of primes that were created my only suggestion is to check your references to see if you include it, and if you added the using for it at the top of your code. 对于无法获取创建的素数的库问题,我唯一的建议是检查引用以查看是否包含它,以及是否在代码顶部添加了它的用法。 I have not used custom libraries in c#, but searching around in google on how to use libraries in c# will be your best bet. 我没有在C#中使用自定义库,但是在Google中搜索如何在C#中使用库将是您最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM