我需要帮助才能在程序中找到素数. [英] I need help to find prime numbers in my program.

查看:80
本文介绍了我需要帮助才能在程序中找到素数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习编程,并且从C#开始.我开始了一个查找和列出素数的项目.我所能做的就是让我的程序从1开始一直计数而不会停止.

I'm trying to learn how to program and I'm starting with C#. I started a project to find and list prime numbers. All I have been able to do is have my program continually count from 1 and up without stopping. 

这是我到目前为止的代码:

This is the code I have so far:

使用系统;

推荐答案

以其中一个示例为例,将其转换为可从代码中调用的方法.给该方法起一个类似"IsPrime"的名称,然后从循环中依次调用每个数字来调用它.

Take one of those examples you have found and turn it into a method that you can call from your code. Give that method a name like "IsPrime", then call it from your loop passing it each number in turn.

哦,您可能应该给循环设置一些限制,所以它不会永远持续下去.

Oh, and you should probably give your loop some limit, so it doesn't keep going for ever.

        static void Main()
        {
            for (int i = 0; i < 100; i++) // Try a reasonable limit like 100. Don't just go on for ever.
            {
                if(IsPrime(i))
                {
                   Console.WriteLine("" + i);
                }
            }
            Console.Read();
        }

        static bool IsPrime(int number)
        {
           // Put your example code in here to test if number 
           // is a prime. Return true if it is, false if it's not.
        }


这篇关于我需要帮助才能在程序中找到素数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆