繁体   English   中英

选择的RadioButton错误

[英]RadioButton being selected is wrong

我在Visual Studio中构建的表单存在一些问题。 每当我输入2个数字并点击“模数”单选按钮时。 它正在自动改变以划分,我不确定为什么。 我已附上我的代码。 我将2个值放入“文本框”,如果选中“详细复选框”,然后单击“计算”。 它执行除法运算,而不是像我所说的那样执行模数运算。

namespace Module10Project
{
    public partial class frmRadioStar : Form
    {

        const byte ADD = 0;
        const byte SUBTRACT = 1;
        const byte MULTIPLY = 2;
        const byte DIVIDE = 3;
        const byte MODULUS = 4;

        public bool isPresent(TextBox txtLeft, TextBox txtRight)
        {
            if(txtLeft.Text == "" || txtRight.Text == "")
            {
                lblMessage.Text = "Please enter a number into the Textbox";
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool divideByZero(TextBox Left, TextBox Right)
        {
            int leftOperand = Convert.ToInt32(Left.Text);
            int rightOperand = Convert.ToInt32(Right.Text);

            if(leftOperand == 0 || rightOperand == 0)
            {
                lblMessage.Text = "Unable to divide by 0";
                return false;
            }
            else
            {
                return true;
            }
        }

        private int equationCalculation(int Left, int Right, byte operation)
        {
            int result = 0;

            if (operation == 0)
                result = Left + Right;
            else if (operation == 1)
                result = Left - Right;
            else if (operation == 2)
                result = Left * Right;
            else if (operation == 3)
                result = Left / Right;
            else if (operation == 4)
                result = Left % Right;
            return result;
        }


        public bool isValid(TextBox Left, TextBox Right)
        {
            if (isPresent(Left, Right) && divideByZero(Left, Right))
                return true;
            else
                return false;
        }
        public frmRadioStar()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtLeft.Text = "";
            txtRight.Text = "";
            lblMessage.Text = "";
            chkVerbose.Checked = true;
            btnAdd.Focus();
            txtLeft.Focus();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if(isValid(txtLeft, txtRight))
                {
                    int result = 0;
                    int leftOperand = Convert.ToInt32(txtLeft.Text);
                    int rightOperand = Convert.ToInt32(txtRight.Text);

                    if (btnAdd.Checked == true && chkVerbose.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 0);
                        lblMessage.Text = leftOperand.ToString() + " + "+ rightOperand.ToString() + " = " + result.ToString();
                    }
                    else if (btnAdd.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 0);
                        lblMessage.Text = "The Answer is: " + result.ToString();
                    }
                    else if(btnSubtract.Checked == true && chkVerbose.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 1);
                        lblMessage.Text = leftOperand.ToString() + " - " + rightOperand.ToString() + " = " + result.ToString();
                    }
                    else if(btnSubtract.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 1);
                        lblMessage.Text = "The answer is: " + result.ToString();
                    }
                    else if(btnMultiply.Checked == true && chkVerbose.Checked == true )
                    {
                        result = equationCalculation(leftOperand, rightOperand, 2);
                        lblMessage.Text = leftOperand.ToString() + " * " + rightOperand.ToString() + " = " + result.ToString();
                    }
                    else if(btnMultiply.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 2);
                        lblMessage.Text = "The answer is: " + result.ToString();
                    }
                    else if(btnDivide.Checked = true && chkVerbose.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 3);
                        lblMessage.Text = leftOperand.ToString() + " / " + rightOperand.ToString() + " = " + result.ToString();
                    }
                    else if(btnDivide.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 3);
                        lblMessage.Text = "The answer is: " + result.ToString();
                    }
                    else if(btnMod.Checked == true && chkVerbose.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 4);
                        lblMessage.Text = leftOperand.ToString() + " % " + rightOperand.ToString() + " = " + result.ToString();
                    }
                    else if(btnMod.Checked == true)
                    {
                        result = equationCalculation(leftOperand, rightOperand, 4);
                        lblMessage.Text = "The answer is: " + result.ToString();
                    }
                }
            }
            catch(Exception ex)
            {

            }
       }
    }
}

我真的不明白您的问题是什么,但可能是因为您输入了一些数字作为其除法结果与模块结果相同的输入。 另外,您在编码中会犯一些错误(例如,定义const值而不使用它们),还有一些未优化的代码(例如if条件)会产生开销。 这是代码的优化版本。 可能是通过澄清代码并调试此版本来解决您的问题:

namespace Module10Project
{
    public partial class frmRadioStar : Form
    {
        enum OperationType
        {
            Add, Subtract, Multiply, Divide, Module
        }

        public bool isPresent(TextBox txtLeft, TextBox txtRight)
        {
            if(txtLeft.Text == "" || txtRight.Text == "")
            {
                lblMessage.Text = "Please enter a number into the Textbox";
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool divideByZero(TextBox Left, TextBox Right)
        {
            int rightOperand = Convert.ToInt32(Right.Text);

            if(rightOperand == 0)
            {
                lblMessage.Text = "Unable to divide by 0";
                return true;
            }
            else
            {
                return false;
            }
        }

        private int equationCalculation(int Left, int Right, OperationType operation)
        {
            switch(operation)
            {
                case OperationType.Add:
                    return Left + Right;
                case OperationType.Subtract:
                    return Left - Right;
                case OperationType.Multiply:
                    return Left * Right;
                case OperationType.Divide:
                    return Left / Right;
                case OperationType.Module:
                    return Left % Right;
            }

            return 0;
        }


        public bool isValid(TextBox Left, TextBox Right)
        {
            return isPresent(Left, Right) && !divideByZero(Left, Right);
        }

        public frmRadioStar()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtLeft.Text = "";
            txtRight.Text = "";
            lblMessage.Text = "";
            chkVerbose.Checked = true;
            btnAdd.Focus();
            txtLeft.Focus();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                if(isValid(txtLeft, txtRight))
                {
                    int result = 0;
                    int leftOperand = Convert.ToInt32(txtLeft.Text);
                    int rightOperand = Convert.ToInt32(txtRight.Text);

                    if (btnAdd.Checked)
                    {
                        result = equationCalculation(leftOperand, rightOperand, OperationType.Add);
                        lblMessage.Text = chkVerbose.Checked ? $"{leftOperand} + {rightOperand} = {result}" : $"The Answer is: {result}";
                    }
                    else if(btnSubtract.Checked)
                    {
                        result = equationCalculation(leftOperand, rightOperand, OperationType.Subtract);
                        lblMessage.Text = chkVerbose.Checked ? $"{leftOperand} - {rightOperand} = {result}" : $"The Answer is: {result}";
                    }
                    else if(btnMultiply.Checked)
                    {
                        result = equationCalculation(leftOperand, rightOperand, OperationType.Multiply);
                        lblMessage.Text = chkVerbose.Checked ? $"{leftOperand} * {rightOperand} = {result}" : $"The Answer is: {result}";
                    }
                    else if(btnDivide.Checked)
                    {
                        result = equationCalculation(leftOperand, rightOperand, OperationType.Divide);
                        lblMessage.Text = chkVerbose.Checked ? $"{leftOperand} / {rightOperand} = {result}" : $"The Answer is: {result}";
                    }
                    else if(btnMod.Checked)
                    {
                        result = equationCalculation(leftOperand, rightOperand, OperationType.Module);
                        lblMessage.Text = chkVerbose.Checked ? $"{leftOperand} % {rightOperand} = {result}" : $"The Answer is: {result}";
                    }
                }
            }
            catch(Exception ex)
            {

            }
       }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM