简体   繁体   English

尝试制作质数检查器并遇到问题

[英]Trying to make a Prime Number Checker And Came across issues

Hi I'm new to coding on C# and thought I should try to make Prime number checker, where you would input a number and it would checked to see if it was prime or not.嗨,我是 C# 编码的新手,我想我应该尝试制作素数检查器,您可以在其中输入一个数字,然后它会检查它是否为素数。 I wanted to try and use functions but came across some issues and I am bit confused.我想尝试使用函数,但遇到了一些问题,我有点困惑。 Also I believe my logic is also wrong but I'm not sure how to fix it.我也相信我的逻辑也是错误的,但我不确定如何解决它。 Here is my code any help is appreciated, also could you explain each part of your logic if you fix it:这是我的代码任何帮助表示赞赏,如果你修复它,你也可以解释你的逻辑的每一部分:

``internal class Program
    {
        static void Main(string[] args)
        {

            int number = Convert.ToInt32(Console.ReadLine());
            IsNumberPrime();

            Console.ReadKey();
        }


        static int IsNumberPrime(int number)
        {
            
            if (number <= 0)
            {
                Console.WriteLine(number + " is " + "not Prime");
            }
            else if (number == 1)
            {
                Console.WriteLine(number + " is " + "not Prime");
            }
            else if (number > 1)
            {
                for (int a = 2; number % a == 0; a++)
                {
                    if (number % a == 0)
                    {

                     Console.WriteLine(number + " is " + "not Prime");
                    }
                    else
                    {
                        Console.WriteLine(number + " is " + " Prime");
                        
                    }
                    
                }
                
            }

           
        }
    }`
 
`

There are several issues in your code, it's completely wrong.您的代码中有几个问题,这是完全错误的。

first I will focus on the logical problems in your code.首先,我将重点关注代码中的逻辑问题。

you are not using the return value, so you can change the return type to void .您没有使用返回值,因此您可以将返回类型更改为void

you should pass number as a parameter to the method IsNumberPrime .您应该将number作为参数传递给方法IsNumberPrime

you should modify the condition in the for a loop.你应该修改for循环中的条件。

you should move the Console.WriteLine(number + " is Prime");你应该移动Console.WriteLine(number + " is Prime"); outside the for loop.for循环之外。

static void IsNumberPrime(int number)
{
    if (number <= 0)
    {
        Console.WriteLine(number + " is not Prime");
    }
    else if (number == 1)
    {
        Console.WriteLine(number + " is not Prime");
    }
    else if (number > 1)
    {
        for (int a = 2; number > a; a++)
        {
            if (number % a == 0)
            {
                Console.WriteLine(number + " is not Prime");
                return;
            }
        }
        Console.WriteLine(number + " is Prime");
    }
}

Now comes to the performance, you can run the loop till the square root of the number, not till the number, which will improve the performance of the code.现在谈到性能,您可以运行循环直到数字的平方根,而不是直到数字,这将提高代码的性能。 also, no need to increment the value of a by 1, you can increment by 2 (first check divide by 2 and then just check divide by the odd numbers, no need to check with the other even numbers).另外,不需要将a的值递增 1,您可以递增 2(首先检查除以 2,然后只检查除以奇数,无需检查其他偶数)。

Here is the optimized code.这是优化后的代码。

static void IsNumberPrime(int number)
{
    if (number <= 1)
    {
        Console.WriteLine(number + " is " + "not Prime");
        return;
    }

    if (number  == 2)
    {
        Console.WriteLine(number + " is " + " Prime");
        return;
    }

    if (number % 2 == 0)
    {
        Console.WriteLine(number + " is " + "not Prime");
        return;
    }
    for (int i = 3; i <= Math.Sqrt(number); i = i + 2)
    {
        if (number % i == 0)
        {
            Console.WriteLine(number + " is " + "not Prime");
            return;
        }
    }

    Console.WriteLine(number + " is " + " Prime");
}

My comments will make your solution run, I didn't check the algorithm for Primes.我的评论将使您的解决方案运行,我没有检查 Primes 的算法。 Your Main function is calling IsNumberPrime() function which expects an integer value in its parameter.您的 Main function 正在调用 IsNumberPrime() function,它期望其参数中有一个 integer 值。 Another remark is that the IsNumberPrime returns an integer value.另一个说明是 IsNumberPrime 返回一个 integer 值。 Because you're logging the results to the console, you should change the return type in the method declaration to void.因为您正在将结果记录到控制台,所以您应该将方法声明中的返回类型更改为 void。 Below are my adjustments to your code:以下是我对您的代码的调整:

static void Main(string[] args)
    {

        int number = Convert.ToInt32(Console.ReadLine());
        IsNumberPrime(number);

        Console.ReadKey();
    }


    static void IsNumberPrime(int number)
    {
        
        if (number <= 0)
        {
            Console.WriteLine(number + " is " + "not Prime");
        }
        else if (number == 1)
        {
            Console.WriteLine(number + " is " + "not Prime");
        }
        else if (number > 1)
        {
            for (int a = 2; number % a == 0; a++)
            {
                if (number % a == 0)
                {

                 Console.WriteLine(number + " is " + "not Prime");
                }
                else
                {
                    Console.WriteLine(number + " is " + " Prime");
                    
                }
                
            }
            
        }

       
    }

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

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