前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
起始数: <asp:TextBox ID="txt_start" runat="server"></asp:TextBox>
结束数:<asp:TextBox ID="txt_end" runat="server"></asp:TextBox>
</br>
提交:<asp:Button ID="Button1" runat="server" Text="计算"
onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
后端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
int i_start = int.Parse(txt_start.Text);
int i_end = int.Parse(txt_end.Text);
int i_beTest;
Boolean b_textReault; string str_reault = "质数有:";
for (i_beTest = i_start; i_beTest <= i_end; i_beTest++)
{
b_textReault = iszhishu(i_beTest);
if (b_textReault == true)
{
str_reault += i_beTest + ",";
}
Label1.Text = str_reault; }
}
protected Boolean iszhishu(int i_beTest)
{
Boolean _result = true;
for(int i= i_beTest/2;i>=2;i--)
{
if(i_beTest%i==0)
{
return false;
break;
}
}
return true;
}
}
执行结果