TeachingNote

web猜数字小游戏

电脑版发表于:2020/11/23 9:34

页面:

<!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>");
                }

            }
        }
    }


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢