分类:
随笔
页面:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> <div style="text-align: center"> <h2 style="color: red">输入一个1-100的数字</h2> <input style="margin-left: 5px" type="text" name="number" /><input type="submit" value="猜一猜" /> <div> <%=msg %> </div> </div> </form> </body> </html>
后台:
public partial class GlassNumber : System.Web.UI.Page { public string msg; protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { if (Session["randomNumber"] == null) { Random random = new Random(); //int randomNumber = random.Next(1, 101); Session["randomNumber"] = random.Next(1, 101); Session["count"] = 0; Session["useTime"] = DateTime.Now; } int randomNumber = Convert.ToInt32(Session["randomNumber"]); //接受前台输入的值 int number = Convert.ToInt32(Request["number"]); Session["count"] = Convert.ToInt32(Session["count"]) + 1; if (number > randomNumber) { Response.Write("<script>alert('你猜的数字比实际的数大')</script>"); } else if (number < randomNumber) { Response.Write("<script>alert('你猜的数字比实际的数小')</script>"); } else { double useTime = (DateTime.Now - DateTime.Parse(Session["useTime"].ToString())).TotalSeconds; msg = "猜对了," + "使用次数:" + Convert.ToInt32(Session["count"]) + "用时" + useTime + "秒"; // Response.Write("<script>alert('猜对了')</script>"); } } } }
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术