MessageBox.Show 用户输入整数时不会出现

MessageBox.Show wont appear when user inputs integers

我正在为学校制作一个基本程序,有一个文本框供用户输入他们的邮政编码,程序应该显示一个消息框,说明根据输入的邮政编码运费是多少。但是当我尝试实现这段代码时,在文本框中输入我的文本并按下提交按钮,消息框没有出现。但另一方面,当我让程序显示一个文本框时,如果有任何不是整数的东西,它就可以正常工作,例如:

                if (textBox1.Text.TrimStart() == string.Empty)
                    MessageBox.Show("A postal code is required");

会正常工作,显示消息框等

                if (textBox1.Text == "test")
                    MessageBox.Show("working");

但是如果涉及到任何整数,程序将什么都不做,甚至不会给出任何错误,这使得这个问题难以解决。比如这样:

                if (textBox1.Text == postcode2string)
                    MessageBox.Show("The cost of shipping is ");

我试过将大括号中的代码分开,而不是像这样将所有代码都放在一个块中:

            {
                if (textBox1.Text == postcode1string)
                    MessageBox.Show("The shipping cost is ");
            }
            {
                if (textBox1.Text == postcode2string)
                    MessageBox.Show("The cost of shipping is ");
            }
            {
                if (textBox1.Text.TrimStart() == string.Empty)
                    MessageBox.Show("A postal code is required");
            }
            {
                if (textBox1.Text == "test")
                    MessageBox.Show("working");
            }

但同样的问题仍然存在我也试过像这样使用else语句

            if (textBox1.Text.TrimStart() == string.Empty)
            {
                MessageBox.Show("A postal code is required");
            }
            else
            {
                textBox1.Text == postcode1string);
                MessageBox.Show("The shipping cost is ");
            }

但这也根本不起作用,我什至将我的整数转换成这样的字符串:

            int postcode1 = 0 - 999;
            int postcode2 = 1000 - 1999;
            int postcode3 = 2000 - 2999;
            int postcode4 = 3000 - 3999;
            int postcode5 = 4000 - 4999;
            int postcode6 = 5000 - 5999;
            int postcode7 = 6000 - 6999;
            int postcode8 = 7000 - 7999;
            string postcode1string = postcode1.ToString();
            string postcode2string = postcode2.ToString();
            string postcode3string = postcode3.ToString();
            string postcode4string = postcode4.ToString();
            string postcode5string = postcode5.ToString();
            string postcode6string = postcode6.ToString();
            string postcode7string = postcode7.ToString();
            string postcode8string = postcode8.ToString();

你也猜到了,还是不行。我所有的问题都没有出错,什么也没有发生。我真的迷路了。 该程序的完整代码是:

        private void button1_Click(object sender, EventArgs e)
        {
            int postcode1 = 0 - 999;
            int postcode2 = 1000 - 1999;
            int postcode3 = 2000 - 2999;
            int postcode4 = 3000 - 3999;
            int postcode5 = 4000 - 4999;
            int postcode6 = 5000 - 5999;
            int postcode7 = 6000 - 6999;
            int postcode8 = 7000 - 7999;
            string postcode1string = postcode1.ToString();
            string postcode2string = postcode2.ToString();
            string postcode3string = postcode3.ToString();
            string postcode4string = postcode4.ToString();
            string postcode5string = postcode5.ToString();
            string postcode6string = postcode6.ToString();
            string postcode7string = postcode7.ToString();
            string postcode8string = postcode8.ToString();

            if (textBox1.Text.TrimStart() == string.Empty)
                MessageBox.Show("A postal code is required");
            if (textBox1.Text == postcode1string)
                MessageBox.Show("The shipping cost is ");
            if (textBox1.Text == postcode2string)
                MessageBox.Show("The cost of shipping is ");
        }

你不能那样声明你的整数。你真正做的是第一个 int Postcode1 等于 0 减去 999。所以它是 -999 并且当你试图说如果例如邮政编码 555 等于 int 它不会因为 555 =! 999.

一种方法是在 if 语句中,您需要满足两个条件。如果它大于 0 且小于 999。您还可以为文本框文本声明一个变量,该变量将是 int,然后在 if 语句中比较它。像这样:

        try{
            int postalcode = int.Parse(textBox1.Text);
        }
        catch{
            MessageBox.Show("Please provide a number")
        }
        
        
        if (postalcode > 0 && postalcode < 999)
            MessageBox.Show("The shipping cost is ");
        if (postalcode > 1000 && postalcode < 1999)
            MessageBox.Show("The cost of shipping is ");