c#窗体应用程序:100以内的素数,每8个数为一行
1.窗体部分:放入一个label控件。(下面代码label的命名为label1)
2.代码部分:
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text = "100以内的素数,每8个数为一行\n\n";
int count = 0;//count变量用来分行
for (int i = 2; i <= 100; i++)
{
int x = 1;
for (int j = 2; j < i; j++)
{
if (i % j == 0)
{
x = 0;
}
}
if (x == 1)
{
count++;
this.label1.Text = this.label1.Text + i + " ";
if (count == 8)
{
this.label1.Text = this.label1.Text + "\n\n";
count = 0;
}
}
}
}
3.运行结果: