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 语句在我的计算器中无法正常工作.需要帮助修复除以 0 错误


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01