0

我用 C# 制作了一个乘法表程序供学生学习。这是我今晚要完成的一个课堂项目。

问题是当我按下“检查答案”按钮时,出现错误“输入字符串的格式不正确”。被抛出。

这是我的代码;这是一个冗长的。检查文本框中的答案是否正确时会引发错误。我还没有找到解决办法。如果您有任何建议或修复,请告诉我。

谢天谢地,克莱顿

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

public partial class ContentPages_MySecondCalculator : System.Web.UI.Page
{
    int SelectedNum = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnTimesTable_Click(object sender, EventArgs e)
    {
        /*protects against incorrect input **/


            int SelectedNum = 0;

            switch (drpNumberList.SelectedValue)
            {
                case "1":
                    SelectedNum = 1;
                    break;
                case "2":
                    SelectedNum = 2;
                    break;
                case "3":
                    SelectedNum = 3;
                    break;
                case "4":
                    SelectedNum = 4;
                    break;
                case "5":
                    SelectedNum = 5;
                    break;
                case "6":
                    SelectedNum = 6;
                    break;
                case "7":
                    SelectedNum = 7;
                    break;
                case "8":
                    SelectedNum = 8;
                    break;
                case "9":
                    SelectedNum = 9;
                    break;
                case "10":
                    SelectedNum = 10;
                    break;
                case "11":
                    SelectedNum = 11;
                    break;
                case "12":
                    SelectedNum = 12;
                    break;

            }

            /* the first list of numbers are set to their values here **/
            lbl1.Text = SelectedNum.ToString();
            lbl1b.Text = SelectedNum.ToString();
            lbl1c.Text = SelectedNum.ToString();
            lbl1d.Text = SelectedNum.ToString();
            lbl1e.Text = SelectedNum.ToString();
            lbl1f.Text = SelectedNum.ToString();
            lbl1g.Text = SelectedNum.ToString();
            lbl1h.Text = SelectedNum.ToString();
            lbl1i.Text = SelectedNum.ToString();
            lbl1j.Text = SelectedNum.ToString();
            lbl1k.Text = SelectedNum.ToString();
            lbl1l.Text = SelectedNum.ToString();

            /* the second list of numbers that appears are set to their values here **/
            lbl2a.Text = "1";
            lbl2b.Text = "2";
            lbl2c.Text = "3";
            lbl2d.Text = "4";
            lbl2e.Text = "5";
            lbl2f.Text = "6";
            lbl2g.Text = "7";
            lbl2h.Text = "8";
            lbl2i.Text = "9";
            lbl2j.Text = "10";
            lbl2k.Text = "11";
            lbl2l.Text = "12";




        }
            // in case no input was taken



    protected void lblNumberList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    /*
    protected void txtBox1_TextChanged(object sender, EventArgs e)
    {

    }
     * */

    protected void btnCheckAnswers_Click(object sender, EventArgs e)
    {
        if (true)
        {
            lblFeedback1.Text = "correct";
        }
        else
        {
            lblFeedback1.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback2.Text = "correct";
        }
        else
        {
            lblFeedback2.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback3.Text = "correct";
        }
        else
        {
            lblFeedback3.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback4.Text = "correct";
        }
        else
        {
            lblFeedback4.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback5.Text = "correct";
        }
        else
        {
            lblFeedback5.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback6.Text = "correct";
        }
        else
        {
            lblFeedback6.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback7.Text = "correct";
        }
        else
        {
            lblFeedback7.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback8.Text = "correct";
        }
        else
        {
            lblFeedback8.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback9.Text = "correct";
        }
        else
        {
            lblFeedback9.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback10.Text = "correct";
        }
        else
        {
            lblFeedback10.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback11.Text = "correct";
        }
        else
        {
            lblFeedback11.Text = "incorrect";
        }
        if (true)
        {
            lblFeedback12.Text = "correct";
        }
        else
        {
            lblFeedback12.Text = "incorrect";
        }

        /*
        if ((Convert.ToInt32(txtBox1.ToString())) == SelectedNum)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 2)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 3)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 4)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 5)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 6)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 7)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 8)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 9)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 10)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 11)
        {

        }
        if ((Convert.ToInt32(txtBox2.ToString())) == SelectedNum * 12)
        {

        }
         * */
    }
    }
4

1 回答 1

1

您得到的错误是当您尝试将无效字符串转换为 int 时。例如:

Convert.ToInt32("asdf"); 

会抛出此错误,因为 asdf 无法转换为有效数字。以下不会抛出此错误:

Convert.ToInt32("123"); 

我建议使用 Int32.TryParse http://msdn.microsoft.com/en-us/library/f02979c7.aspx

这个函数是专门为了防止抛出异常而引入的。以下是它的用法示例:

  int number;
  bool result = Int32.TryParse(value, out number);
  if (result)
  {
     Console.WriteLine("Converted '{0}' to {1}.", value, number);         
  }
  else
  {
     if (value == null) value = ""; 
     Console.WriteLine("Attempted conversion of '{0}' failed.", value);
  }
于 2013-04-26T00:42:51.473 回答