素数C# [英] prime numbers c#

查看:147
本文介绍了素数C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和我想的东西像编程,但在一个列表框显示的质数,如果用户在文本框中输入任意整数。 (这意味着,如果他们写10,它将从0-10,或0-20 20等显示质数)。



我应该首先考虑,以前我做节目?
我知道有在互联网上的例子很多,但首先我想知道我还需要什么的提示



感谢; - ?)



===
谢谢你们。所以你暗示,最好先做它在控制台应用程序?
我做的For循环使用控制台应用程序一个非常简单的例子,但后来当我试图做到这一点在Windows窗体应用程序,我不知道如何实现它。
我怕如果我继续在控制台做例子,那我就很难做到这一点在Windows窗体应用程序。
你觉得呢?



==
又见面了,



我需要我的代码一些反馈:

  Console.WriteLine(请输入您的整数:); 
长yourInteger;
yourInteger = Int32.Parse(到Console.ReadLine());

//显示第一个素数,比较它的定整
为(长I = 2; I< = yourInteger; I = I + 1)
{
//控制我如果它的质数或没有
如果((我%2!= 0)||(我== 2))
{
Console.Write( {0},我);
}

}


解决方案

好,首先我会思考如何寻找素数,并写在一个控制台应用程序,读取一行,做数学,并写入结果(纯粹是因为这是你可以做最简单的事情,并覆盖相同的解析等你以后就需要逻辑)



当你很高兴与素数生成,然后看看怎么做的WinForms - 如何把一个列表框,文本框和按钮形式;如何处理点击事件(的按钮),以及如何从文本框读取和写入值到列表框中。您的首要代码应该是相当确定采取现况...



如果您还没有一个IDE,然后记下的 C#快递是免费的,将覆盖所有上述


And I would like to program something like, displaying the prime numbers in a listbox if user will input any integer in the textbox. (that means, if they write 10, it will display the prime numbers from 0-10, or 20 from 0-20, etc).

What should I consider first, before I do the programming? I know there are many examples in the internet, but first I would like to know what will I need?

Thanks for the tip;-)

=== Thanks guys. So you're suggesting that it's better to do it first in the Console application? I did an example of "For Loop" using Console Application a very simple one, but then when I tried to do it in the Windows Form Application, I'm not sure how to implement it. I'm afraid that if I keep doing examples in the Console, then I'll have difficulty to do it in Windows Form Apps. What do you think?

====== Hello again,

I need some feedback with my code:

        Console.WriteLine("Please enter your integer: ");
        long yourInteger;
        yourInteger = Int32.Parse(Console.ReadLine());

        //displaying the first prime number and comparing it to the given integer
        for (long i = 2; i <= yourInteger; i = i + 1)
        {
            //Controls i if its prime number or not
            if ((i % 2 != 0) || (i == 2))
            {
                Console.Write("{0} ", i);
            }

        }

解决方案

Well, first of all I'd think about how to find prime numbers, and write that in a console app that reads a line, does the math, and writes the results (purely because that is the simplest thing you can do, and covers the same parsing etc logic you'll need later).

When you are happy with the prime number generation, then look at how to do winforms - how to put a listbox, textbox and button on a form; how to handle the click event (of the button), and how to read from the textbox and write values into the listbox. Your prime code should be fairly OK to take "as is"...

If you don't already have an IDE, then note that C# Express is free and will cover all of the above.

这篇关于素数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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