我的 switch 语句中的 if 语句在我的计算器中无法

my if statement within my switch statement does not work correct within my calculator. need help fixing the divide by 0 error(我的 switch 语句中的 if 语句在我的计算器中无法正常工作.需要帮助修复除以 0 的错误)

本文介绍了我的 switch 语句中的 if 语句在我的计算器中无法正常工作.需要帮助修复除以 0 的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
        }
    }
}
}

推荐答案

在aequal_Click(object sender, RoutedEventArgs e)的第一行替换

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

Double num02 = 0;

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

这篇关于我的 switch 语句中的 if 语句在我的计算器中无法正常工作.需要帮助修复除以 0 的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我的 switch 语句中的 if 语句在我的计算器中无法

基础教程推荐