N
N
neitoo2022-01-29 12:53:12
C++ / C#
neitoo, 2022-01-29 12:53:12

How, when dividing by 0, to display the phrase in the textbox beyond the place of infinity?

'=' button

private void button17_Click(object sender, EventArgs e)
        {
            Double Value2 = Convert.ToDouble(textBox1.Text);
            Double Result = Value2;
            switch (Oper)
            {
                case 1:
                    Result = Value + Value2;
                    break;
                case 2:
                    Result = Value - Value2;
                    break;
                case 3:
                    Result = Value * Value2;
                    break;
                case 4:
                    if (Value2 == 0)
                    {
                        textBox1.Text = "На 0 делить нельзя!";
                    }
                    else
                    {
                        Result = Value / Value2;
                    }
                    break;
            }
            textBox1.Text = Math.Round(Result,4).ToString();
            Value = 0;
            Oper = 0;

        }


Button '/'
private void button4_Click(object sender, EventArgs e)
        {
            button17_Click(sender, e);
            Value = Convert.ToDouble(textBox1.Text);
            Oper = 4;
            textBox1.Text = "0";
        }


In case 4, if if else is removed, it will display infinity, and if with if else, then it simply remains 0 in the textBox

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Shporkhun, 2022-02-02
@xJAYx

Something like this.

private void button17_Click(object sender, EventArgs e)
        {
            Double Value2 = Convert.ToDouble(textBox1.Text);
            Double Result = Value2;
            switch (Oper)
            {
                case 1:
                    Result = Value + Value2;
                    break;
                case 2:
                    Result = Value - Value2;
                    break;
                case 3:
                    Result = Value * Value2;
                    break;
                case 4:
                    if (Value2 == 0)
                    {
                        textBox1.Text = "На 0 делить нельзя!";
                    }
                    else
                    {
                        Result = Value / Value2;
                        textBox1.Text = Math.Round(Result, 4).ToString();
                    }
                    break;
            }
            Value = 0;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Value = Convert.ToDouble(textBox1.Text);
            Oper = 4;
            textBox1.Text = "0";
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question