代码和运行窗口已经截图
num1,num2分别为第一个第二个整数,运行代码后,无输出结果,而且窗口卡住
不能写在Text3_Changed里,要写在按钮的click里
你在Text3_Changed里修改Text3.Text,导致又触发了新的Text3_Changed,如此这般,最后导致堆栈溢出。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Q714037
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int a = int.Parse(num1.Text);
int b = int.Parse(num2.Text);
for (int i = a; i <= b; i++)
{
bool p = true;
for (int j = 2; j <= i / 2; j++)
if (i % j == 0) { p = false; break; }
if (p && i >= 2) num3.Text += i.ToString() + " ";
}
}
private void button2_Click(object sender, EventArgs e)
{
num3.Text = "";
}
}
}
窗体设计文件:
namespace Q714037
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.num1 = new System.Windows.Forms.TextBox();
this.num2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.num3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(126, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(115, 13);
this.label1.TabIndex = 0;
this.label1.Text = "求两个数之前的素数";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(19, 52);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(67, 13);
this.label2.TabIndex = 1;
this.label2.Text = "第一个整数";
//
// num1
//
this.num1.Location = new System.Drawing.Point(92, 49);
this.num1.Name = "num1";
this.num1.Size = new System.Drawing.Size(86, 20);
this.num1.TabIndex = 2;
//
// num2
//
this.num2.Location = new System.Drawing.Point(279, 49);
this.num2.Name = "num2";
this.num2.Size = new System.Drawing.Size(86, 20);
this.num2.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(206, 52);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(67, 13);
this.label3.TabIndex = 3;
this.label3.Text = "第二个整数";
//
// num3
//
this.num3.Location = new System.Drawing.Point(20, 89);
this.num3.Multiline = true;
this.num3.Name = "num3";
this.num3.Size = new System.Drawing.Size(344, 177);
this.num3.TabIndex = 5;
//
// button1
//
this.button1.Location = new System.Drawing.Point(92, 280);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 22);
this.button1.TabIndex = 6;
this.button1.Text = "找素数";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(236, 280);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 22);
this.button2.TabIndex = 7;
this.button2.Text = "清除";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(390, 323);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.num3);
this.Controls.Add(this.num2);
this.Controls.Add(this.label3);
this.Controls.Add(this.num1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox num1;
private System.Windows.Forms.TextBox num2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox num3;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
运行截图