如何打印素数?

编程入门 行业动态 更新时间:2024-10-28 04:25:34
本文介绍了如何打印素数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用此代码代码

i'am using this code code

protected void Button1_Click(object sender, EventArgs e) { int z = Convert.ToInt32(TextBox1.Text); for (int x = 2; x <= z; x++) { int isprime = 0; for (int y = 1; y < x; y++) { if (x % y == 0) isprime++; if (isprime == 2) break; } if (isprime != 2) Label2.Text =Convert.ToString(x); isprime = 0; } }

我的尝试: 在这种情况下,最大素数来自输出,但是我需要输出中的所有素数达到最大数量? 为了获得我所需的输出我会采取什么样的改变? 问候, nagarjuna

What I have tried: In this case biggest prime number is coming from the output,but i need all prime numbers up to maximum number printing in the output?. what changes i'am taking to get my required output? regards, nagarjuna

推荐答案

当你写: When you write: Label2.Text =Convert.ToString(x);

在一个循环中,你只会得到最后使用的值,就像你说的那样:

Inside a loop, you will only ever get teh last value to used, just teh same as if you said:

int x = 0; for (int i = 0; i < 10; i++) { x = i; }

X总是等于循环中的最终值:9。 你可以用字符串做你想做的事:

X will always equal the final value in the loop: 9. You can do what you want with strings:

Label2.Text += Convert.ToString(x);

但这不是一个好主意,因为字符串是不可变的 - 每次添加到字符串时都会创建一个新字符串字符串,并将旧数据和新数据复制到其中。这是非常低效的,特别是当你生成大量数字时! 更好的解决方案是使用StringBuilder:

But that's not a good idea, as string are immutable - every time you add to a string you create a new string, and copy the old and new data into it. That's pretty inefficient, particularly when you generate a lot of numbers! A better solution is to use a StringBuilder:

StringBuilder sb = new StringBuilder(); string sep = ""; for (int i = 0; i < 10; i++) { sb.AppendFormat("{0}{1}", sep, i); sep = ", "; }

然后使用循环后的输出:

And then use the output of the that after the loop:

Label2.Text = sb.ToString();

但是要添加几个细节: 1)不要使用Convert作为输入或输出 - 它有一个地方,但这不是它!当您在用户输入上使用Convert.ToInt32时,如果用户输入错误,您的应用程序将崩溃。 这里更好的解决方案是使用TryParse:

But a couple of details to add: 1) Don't use Convert for input or output - it has a place, but this isn't it! When you use Convert.ToInt32 on user input, you app will crash if the user mistypes. A much better solution here is to use TryParse:

int z; if (!int.TryParse(TextBox1.Text, out z)) { // User mistyped report problem to user return; }

并且无需使用转换输出:

And there is no need to use Convert for output:

... = x.ToString;

更清楚了! 2)帮自己一个忙,并停止使用Visual Studio默认名称 - 你可能还记得TextBox8是今天的手机号码,但是当你必须在三周后修改它,你呢?使用描述性名称 - 例如tbMobileNo - 您的代码变得更容易阅读,更自我记录,更易于维护 - 并且编码速度更快,因为Intellisense可以通过三次击键来tbMobile,其中TextBox8需要思考大概和8次击键... 3)停止使用单个字符变量名称!使用描述变量用于解释变量的描述性名称使得代码更容易阅读和理解(并且通常与单个字符一样快,这要归功于Intellisense)。

Is a lot clearer! 2) Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes... 3) Stop using single character variable names! Using descriptive names which explain what the variable is used for makes your code a lot easier to read and understand (and generally is as quick as single character ones thanks to Intellisense).

试试这个,请参阅解决方案1以获得解释.. try this, refer Solution 1 for the explanation.. if (isprime != 2) ListBox1.Items.Add(x.ToString()); isprime = 0;

更多推荐

如何打印素数?

本文发布于:2023-10-20 22:13:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!