【问题标题】:Improving number guessing game code改进猜数字游戏代码
【发布时间】:2023-03-20 03:30:01
【问题描述】:

我正在尝试制作一个小型猜谜游戏,它会生成一个随机数,用户使用TextBoxButton 输入一个数字。目前它正在创建随机数并做我想做的一切,但是每次用户按下按钮上的提交时,它都会生成一个新的随机数供他们猜测

我对 ASP.NET 类型的东西很陌生,所以我的代码可能效率低下且不正确,哈哈,所以我有两个特别的问题。

  1. 如何改进我的代码以使其更好地工作/完全正常工作。
  2. 我是否需要一个跑步者课程才能让它工作,我该怎么做?
public partial class WebPageSeparated : System.Web.UI.Page
{
    private int randNum;
    private int theirGuess;
    public WebPageSeparated()
    {
        Random randomNum = new Random();
        randNum = randomNum.Next(0, 10);
        theirGuess = 0;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "Guessing game! Guess a number between [0,10) to see if you can get it right!";
        new WebPageSeparated();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
       try
        {
            theirGuess = Convert.ToInt32(TextBox1.Text);
            if (theirGuess != this.randNum)
            {
                Label1.Text = "Sorry, wrong number.  Please try again!";
            }
            else if(theirGuess == this.randNum)
            {
                Label1.Text = "Correct! A new number has been generated, go ahead and try to do it again!";
                new WebPageSeparated();
            }
        }
        catch (System.FormatException)
        {
            Label1.Text = "Enter a number [1,10)";
        }
    }
}

【问题讨论】:

  • 所以当他们点击提交时,它不会返回这两个响应之一?
  • @LucasKot-Zaniewski 确实如此,但是每次您提交新号码时,应该猜到的号码都会改变
  • 第一步:不要使用异常来控制程序流程。

标签: c# asp.net postback viewstate page-lifecycle


【解决方案1】:

您的代码有几个问题:

您永远不应创建页面的新实例 (new WebPageSeparated())。每次您导航到该页面(通过在浏览器中输入其 URL)以及每当您导致 PostBack(例如通过单击 asp:button)时,都会创建一个新实例。

如果您想拥有一些仅在第一次调用页面时运行的代码(即不在 PostBack 期间,仅在导航到页面时运行),那么您应该将该代码包装在 if (!IsPostBack) {} 块中。

因为每个 PostBack 都会创建一个新的页面实例,所以您不能在页面的实例字段中存储任何状态(随机数)。您必须找到另一个存储状态的地方,例如:

  • 在静态字段中,例如:private static int randNum
    • 注意:不建议这样做,因为静态字段在您页面的所有实例之间共享(如果多个用户同时在您的网站上,则不起作用)
  • 在会话变量中,例如Session["randNum"] = randomNum.Next(0, 10)
  • 在页面的ViewState中,例如ViewState["randNum"] = ...
    • 这是我为您的示例推荐的内容
  • 在数据库中

考虑到所有这些要点,您的 Page_Load 方法将如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Random randomNum = new Random();
            randNum = randomNum.Next(0, 10);
            ViewState["randNum"] = randNum;
        }
        else
        {
            randNum = (int) ViewState["randNum"];
        }
        Label1.Text = "Guessing game! Guess a number between [0,10) to see if you can get it right!";
    }

另外,如果用户猜对了数字,您应该生成并存储一个新的随机数(就像在if (!IsPostBack) 中所做的那样)。


最后,一些您可能想详细了解的主题:页面生命周期、回发、视图状态、会话状态

【讨论】:

    【解决方案2】:

    原因是 asp.net postback 将浏览器中执行的每个操作的所有数据发送到服务器。由于您在 constructor 中生成一个随机数,因此您面临这个问题。我想这会对你有所帮助。

    public partial class WebPageSeparated : System.Web.UI.Page
    
    {
    
    private int randNum;
    private int theirGuess;
    
    
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Random randomNum = new Random();
        randNum = randomNum.Next(0, 10);
        theirGuess = 0;
        Label1.Text = "Guessing game! Guess a number between [0,10) to see if you can get it right!";
        new WebPageSeparated();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
           try
            {
                theirGuess = Convert.ToInt32(TextBox1.Text);
                if (theirGuess != this.randNum)
                {
                    Label1.Text = "Sorry, wrong number.  Please try again!";
                }
                else if(theirGuess == this.randNum)
                {
                    Label1.Text = "Correct! A new number has been generated, go ahead and try to do it again!";
                    new WebPageSeparated();
                }
            }
            catch (System.FormatException)
            {
                Label1.Text = "Enter a number [1,10)";
            }
        }
    

    【讨论】:

    • 我尝试了您上面的代码但没有修复它,我做到了,所以当它说“再试一次”时,它会打印出随机数是什么,并且每次我按下按钮上的提交数字变了..
    • 我刚才注意到新的 WebPageSeparated() 实例化是在事件处理程序内部完成的,这不是必需的。但我想知道你为什么每次都实例化。
    猜你喜欢
    • 2021-11-20
    • 2016-06-21
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多