如何打印素数? [英] How to print prime numbers?
问题描述
我使用此代码代码
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;
这篇关于如何打印素数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!