C# switch语句中的if语句在计算器中无法正常工作。需要帮助修复“除以0”错误吗

C# switch语句中的if语句在计算器中无法正常工作。需要帮助修复“除以0”错误吗,c#,if-statement,webforms,switch-statement,calculator,C#,If Statement,Webforms,Switch Statement,Calculator,我创建了一个基本的计算器,但我用除法对它进行了编码,当我除以0时,它会在第二个文本框中给用户提供错误,但现在即使我除以3或任何其他不是0的数字,错误仍会出现在我的第二个文本框中 namespace calc { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public seal

我创建了一个基本的计算器,但我用除法对它进行了编码,当我除以0时,它会在第二个文本框中给用户提供错误,但现在即使我除以3或任何其他不是0的数字,错误仍会出现在我的第二个文本框中

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;
        }
    }
}
}
名称空间计算
{
/// 
///可以单独使用或在框架内导航到的空页。
/// 
公共密封部分类主页面:第页
{
双num01=0;
整数运算=0;
公共主页()
{
this.InitializeComponent();
}
私有void num0\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+0;
}
私有void num1\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+1;
}
私有void num2\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+2;
}
私有void num3\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+3;
}
私有void num4\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+4;
}
私有void num5_单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+5;
}
私有void num6\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+6;
}
私有void num7_单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+7;
}
私有void num8\u单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+8;
}
私有void num9_单击(对象发送方,路由目标)
{
textbox1.Text=textbox1.Text+9;
}
私有void aclear_Click(对象发送者,路由目标e)
{
textbox1.Text=String.Empty;
操作=0;
num01=0;
}
私有无效点点击(对象发送者,路由目标)
{
textbox1.Text=Convert.ToString(textbox1.Text+“,”);
}
私有无效adiv_单击(对象发送者,路由目标e)
{
num01=Convert.ToDouble(textbox1.Text);
textbox1.Text=“0”;
操作=1;
}
私有无效多重单击(对象发送者,路由目标)
{
num01=Convert.ToDouble(textbox1.Text);
textbox1.Text=“0”;
操作=2;
}
私有无效asub_单击(对象发送者,路由目标e)
{
num01=Convert.ToDouble(textbox1.Text);
textbox1.Text=“0”;
操作=3;
}
私有void aadd_单击(对象发送方,路由目标)
{
num01=Convert.ToDouble(textbox1.Text);
textbox1.Text=“0”;
操作=4;
}
私有无效相等单击(对象发送者、路由目标)
{
双num02=0;
开关(操作)
{
案例1:
如果(num02==0)
{
textbox2.Text=“不能被零除”;
textbox1.Text=String.Empty;
}
其他的
{
textbox1.Text=Convert.ToString(num01/num02);
}
打破
案例2:
textbox1.Text=Convert.ToString(Convert.ToDouble(textbox1.Text)*num01);
打破
案例3:
textbox1.Text=Convert.ToString(num01-Convert.ToDouble(textbox1.Text));
打破
案例4:
textbox1.Text=Convert.ToString(Convert.ToDouble(textbox1.Text)+num01);
打破
}
}
}
}

在aequal\u的第一行单击(对象发送器,路由目标e)替换


将num02设置为0,然后检查它是否为0。num02似乎是一个局部变量,您将num02的值初始化为0,然后检查它是否为零并报告错误。为了说明显而易见的情况,您需要将num02设置为零以外的值。我希望num02在用户按下按钮时成为用户输入。它是我的num01,然后是我的运算符,我希望num02作为用户定义的数字。你能帮我吗?我只希望在用户除以零时出现错误,当用户除以另一个值时,您将得到结果textbox1@grootpoot:请尝试我的答案,我认为它解决了您的问题。@PaulF:我不这么认为,因为用户可能在按下一个运算符后在文本框中输入了不同的数字。就像任何计算器一样。很抱歉,我没有注意到计算是在“相等”按钮上单击的。谢谢奥利弗,它成功了。我可以在评论中问你们另一个问题吗?如果它与这个问题无关,那么你们应该发布another@grootpoot当前位置如果它起作用,我将非常感谢您勾选我的答案;-)
Double num02 = 0;
Double num02 = Convert.ToDouble(textbox1.Text);