如果直接跳到其他地方C#

时间:2015-03-19 14:35:11

标签: c# if-statement

单击按钮,我将用户的文本框输入存储到int变量“rc”中。之后我宣布了if语句。 问题是,即使用户确实输入了3或5或7或任何这些选项,它也只会跳到else并显示随附的MessageBox。 为什么会这样?这是我的代码,任何帮助将不胜感激,提前感谢。

int rc;
double centro, centro1;
int r2, c2, rc2;
private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.Clear();
    try
    {
        rc = Convert.ToInt16(textBox1.Text);
        if (rc == 3 || rc == 5 || rc == 7 || rc == 9 || rc == 11)
        {
            centro = rc / 2;
            centro1 = Math.Round(centro, 0);
            int[,] dim = new int[rc, rc];
            int v = 1, r = 0, c, x = 0;
            c = Convert.ToInt16(centro1);
            for (x = 0; x < (rc*rc); x++)
            {
                if (dim[r, c] >= 1)
                {
                    r = r2 + 2;
                    c = c2 - 1;
                    dim[r, c] = v;
                }
                else
                {
                    dim[r, c] = v;
                }

                c++;
                r--;
                v++;
                if (r < 0)
                {
                    r = rc -1;
                };

                if (c > (rc-1))
                {
                    c = 0;
                };

                r2 = r;
                c2 = c;   
            }

            string matrixString = "";
            for (int i = 0; i < dim.GetLength(0); i++)
            {
                for (int j = 0; j < dim.GetLength(1); j++)
                {
                     matrixString += dim[i, j].ToString();
                     matrixString += " ";
                }

                matrixString += Environment.NewLine;
            }

            this.richTextBox1.Text = matrixString;
        }
        else
        {
            textBox1.Text = "";
            MessageBox.Show("***Error*** 1...");
        }
    }
    catch
    {
        textBox1.Text = "";
        MessageBox.Show("***Error*** 2...");
    }
}

1 个答案:

答案 0 :(得分:3)

您没有看到MessageBox区块中的else。相反,您会看到catch区块中的那个。

请参阅下面的更改代码以获取证据,使用Console.WriteLine代替MessageBoxstring代替textBox.Text,最后更改错误消息,以便它们指示哪个块他们来自:

using System;

public class Test
{
    public static void Main()
    {
        new Test().button1_Click(null, null);
    }

    int rc;
    double centro, centro1;
    int r2, c2, rc2;
    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            string textBoxContents = "9";
            rc = Convert.ToInt16(textBoxContents);
            if (rc == 3 || rc == 5 || rc == 7 || rc == 9 || rc == 11)
            {
                centro = rc / 2;
                centro1 = Math.Round(centro, 0);
                int[,] dim = new int[rc, rc];
                int v = 1, r = 0, c, x = 0;
                c = Convert.ToInt16(centro1);
                //rc2 = rc;
                for (x = 0; x < (rc*rc); x++)
                {
                    if (dim[r, c] >= 1)
                    {
                        r = r2 + 2;
                        c = c2 - 1;
                        dim[r, c] = v;
                    }
                    else
                    {
                        dim[r, c] = v;
                    }
                    c++;
                    r--;
                    v++;
                    if (r < 0)
                    {
                        r = rc -1;
                    };
                    if (c > (rc-1))
                    {
                        c = 0;
                    };
                    r2 = r;
                    c2 = c;

                }
                string matrixString = "";
                for (int i = 0; i < dim.GetLength(0); i++)
                {
                    for (int j = 0; j < dim.GetLength(1); j++)
                    {
                        matrixString += dim[i, j].ToString();
                        matrixString += " ";
                    }

                    matrixString += Environment.NewLine;
                }
                Console.WriteLine(matrixString);
            }
            else
            {
                Console.WriteLine("***Else***   Verifica que:\n- Introduzcas   solo digitos.\n- Introduzcas solo numeros inpares\ndentro de las demensiones indicadas.\n- Solo introduzcas el numero de\nrenglones o columnas.");
            }
        }

        catch
        {
            Console.WriteLine("***Exception***   Verifica que:\n- Introduzcas solo      digitos.\n- Introduzcas solo numeros inpares\ndentro de las demensiones indicadas.\n- Solo introduzcas el numero de\nrenglones o columnas.");
        }
    }

}

看到它正在运行here