1

我有两个问题:

1)这是使变量的默认值= 0,然后将用户给定的值传递给该变量的正确方法吗?

protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer

2)如何阻止 Check_Click(提交按钮)隐藏 txtRemainder(文本框)?它现在“自动隐藏”的原因是因为我将默认值设置为“txtRemainder.Visible = false;” 在 Page_Load 中,除非数学问题是除法,否则它将使其隐藏。当我单击 btnDiv_Click(divide button) 时,它会将其重置为“txtRemainder.Visible = true;”,因为这提供了一个待解决的除法问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BasicMath : System.Web.UI.Page
{
    int number1;
    int number2;
    int answer;

    protected void Page_Load(object sender, EventArgs e)
    {
        txtRemainder.Visible = false;
    }

    protected void GetRandom()
    {
        Random rand = new Random();
        number1 = rand.Next(0, 10);
        number2 = rand.Next(0, 10);
        txtAnswer.Text = "";
        txtRemainder.Text = "";
        lblYesNo.Text = "";
    }//END Get Random Number

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " + " + number2.ToString();
        answer = number1 + number2;
        Session["answer"] = answer;
    }//END Addition Button

    protected void btnSub_Click(object sender, EventArgs e)
    {
        GetRandom();
        if (number2 > number1)
        {
            answer = number2 - number1;
            lblEquation.Text = number2.ToString() + " - " + number1.ToString();
        }
        else
        {
            answer = number1 - number2;
            lblEquation.Text = number1.ToString() + " - " + number2.ToString();
        }
        Session["answer"] = answer;

    }//END Subtraction Button

    protected void btnMult_Click(object sender, EventArgs e)
    {
        GetRandom();
        lblEquation.Text = number1.ToString() + " x " + number2.ToString();
        answer = number1 * number2;
        Session["answer"] = answer;
    }//END Multiplication Button

    protected void btnDiv_Click(object sender, EventArgs e)
    {

        Random rand = new Random();
        number1 = rand.Next(1, 10);
        number2 = rand.Next(1, 10);
        /*will only display the txtRemainder(textbox) while using the Divide button,
           txtRemainder will auto-hide when using another button because
           it's default setting of '.Visible = false' is placed in the Page_Load*/
        txtRemainder.Visible = true;
        lblEquation.Text = number1.ToString() + " / " + number2.ToString(); 
        answer = number1 / number2;
        Session["answer"] = answer;

    }//END Division Button

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        lblYesNo.Text = "";
        //default int values are set to 0
        int remainder = 0;
        int guess = 0;

        remainder = int.Parse(txtRemainder.Text);

        guess = int.Parse(txtAnswer.Text);
        answer = (int)Session["answer"];
        if (guess == answer)
        {
            lblYesNo.Text = lblYesNo.Text + "Correct!";
        }
        else
        {
            lblYesNo.Text = lblYesNo.Text + "Try Again..";
        }
    }//END Check Answer
}

如果这个问题没有意义,请要求澄清。

4

2 回答 2

1

问题一:

是的,这是将变量初始化为零默认值的正确方法,尽管int变量默认初始化为零,因此即使您省略了赋值,它们仍将默认为零。

您用于接受用户输入的方法是正确的,但如果用户输入的值不是 Int32 值(想想 3.2),则可能会引发异常。就目前而言,您没有 try..catch 块来处理这些异常。您可以添加这些 try..catch 块,也可以使用 TryParse() 方法检查这些值是否有效。例如:

protected void btnCheck_Click(object sender, EventArgs e)
{
    lblYesNo.Text = "";
    //default int values are set to 0
    int remainder = 0;
    int guess = 0;

    if (!Int32.TryParse(txtRemainder.Text, out remainder))
    {
        // do something here to inform the user that remainder is invalid
        return;
    }

    if (!Int32.TryParse(txtAnswer.Text, out remainder))
    {
        // do something here to inform the user that answer is invalid
        return;
    }

    answer = (int)Session["answer"];
    if (guess > answer)
    {
        lblYesNo.Text = lblYesNo.Text + "Try Again..";
    }
    else if (guess < answer)
    {
        lblYesNo.Text = lblYesNo.Text + "Try Again..";
    }
    else
    {
        lblYesNo.Text = lblYesNo.Text + "Correct!";
    }
}//END Check Answer

TryParse() 会将值转换为输出变量,如果它是有效值并返回以指示成功,否则如果无法执行转换true,它将返回。false

问题2:

为了txtRemainder在页面首次加载时隐藏,然后在单击除 之外的任何按钮时使其隐藏btnDiv,这是一个建议的解决方案。首先,代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack) {
        lblTest.Visible = false;
    } else {
        foreach (string ctrl in Request.Form) {
            Control c = FindControl(ctrl);
            if (c is Button) {
                txtRemainder.Visible = c.ID == "btnDiv";
                return;
            }
        }
    }
}

现在进行一些解释。当页面最初加载时,标签是隐藏的(通过!IsPostBack检查)。如果请求确实是回发,我们会检查返回的表单控件集合(包含在Request.Form集合中)并检查是否存在可能导致回发的按钮。按钮在 HTML 中作为元素呈现,并且在回发时,即使页面上有多个提交,也<input type="submit" .. />只有被单击的按钮会被发送回集合中。Form更详细的解释可以在这里找到

现在,如果一个按钮确实导致了回发,那么我们检查这个按钮是否是btnDiv. 如果是,则比较返回true&txtRemainder并显示。如果没有,它是隐藏的。

这样,就不需要txtRemainder在每个事件处理程序中显示或隐藏。您甚至不需要在btnDiv.

编辑

根据评论中列出的要求,我更改了对问题 2 的回答,以便提供一种替代方法,无需在每个按钮事件中显示或隐藏标签。

于 2012-12-08T00:01:10.160 回答
0

问题1: 如果您使用.Net 4.0 及更高版本,请考虑使用该TryParse方法,并检查结果是否成功,这样您可以更好地控制解析值和捕获异常,例如

int number = 0;
bool result = false;

result = int.TryParse("123", out number);

if (!result)
{
    throw new InvalidCastException("Error converting number");
}

问题2: 看起来您正在使用Asp.Net,所以尝试Page.IsPostBack在Page_Load 事件中进行测试,这样您就可以确定页面加载是因为初始加载,还是因为您向服务器发送了数据,即单击了一个按钮。

希望这可以帮助!

于 2012-12-08T00:01:19.973 回答