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 的错误)
问题描述
我创建了一个基本的计算器,但是我对它进行了除法编码,当我除以零时,它会在第二个文本框中向用户显示错误,但现在即使我除以 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 语句在我的计算器中无法
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30