my if statement within my switch statement does not work correct within my calculator. need help fixing the divide by 0 error

跟風遠走 提交于 2019-12-20 07:46:29

问题


i created a basic calculator, but with division i coded it, that when i divide by zero it will give the error to the user in the 2nd textbox, but now even if i divide by 3 or any other number that is not 0, the error keeps appearing in my second textbox.

namespace calc
 {
/// <summary>
/// An empty page that can be used on its own or navigated to within a     Frame.
    /// </summary>
     public sealed partial class MainPage : Page
    {
    Double num01 = 0;

    int Operation = 0; 
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void num0_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "0";
    }

    private void num1_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "1";
    }

    private void num2_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "2";
    }

    private void num3_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "3";
    }

    private void num4_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "4";
    }

    private void num5_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "5";
    }

    private void num6_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "6";
    }

    private void num7_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "7";
    }

    private void num8_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "8";
    }

    private void num9_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = textbox1.Text + "9";
    }

    private void aclear_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = String.Empty;
        Operation = 0;
        num01 = 0;
    }

    private void ppoint_Click(object sender, RoutedEventArgs e)
    {
        textbox1.Text = Convert.ToString (textbox1.Text + ",");
    }

    private void adiv_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(textbox1.Text);
        textbox1.Text = "0";
        Operation = 1;
    }

    private void amultiply_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(textbox1.Text);
        textbox1.Text = "0";
        Operation = 2;
    }

    private void asub_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(textbox1.Text);
        textbox1.Text = "0";
        Operation = 3;
    }

    private void aadd_Click(object sender, RoutedEventArgs e)
    {
        num01 = Convert.ToDouble(textbox1.Text);
        textbox1.Text = "0";
        Operation = 4;
    }

    private void aequal_Click(object sender, RoutedEventArgs e)
    {
        Double num02 = 0;

        switch (Operation)
        {
            case 1:
                if (num02 == 0)
                {
                    textbox2.Text = "Can not divide by zero";
                    textbox1.Text = String.Empty;

                }
                else
                {
                    textbox1.Text = Convert.ToString(num01 / num02); 
                }

                break;
            case 2:
                textbox1.Text = Convert.ToString(Convert.ToDouble    (textbox1.Text) * num01);
                break;
            case 3:
                textbox1.Text = Convert.ToString(num01-     Convert.ToDouble        (textbox1.Text));
                break;
            case 4:
                textbox1.Text = Convert.ToString(Convert.ToDouble     (textbox1.Text) + num01);
                break;
        }
    }
}
}

回答1:


On the first line of aequal_Click(object sender, RoutedEventArgs e) replace

Double num02 = 0;

by

Double num02 = Convert.ToDouble(textbox1.Text);


来源:https://stackoverflow.com/questions/48869871/my-if-statement-within-my-switch-statement-does-not-work-correct-within-my-calcu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!