C# 为什么我的计算器里的计算不正确?
对于我的C#类,我们需要编写一个简单的计算器来执行基本的数学运算(加、减、乘、除)。我看了教授的视频,他的节目很好。减法命令似乎在结果前面加了一个-。divide命令的结果有点不对劲。我做错了什么 例如:C# 为什么我的计算器里的计算不正确?,c#,.net,C#,.net,对于我的C#类,我们需要编写一个简单的计算器来执行基本的数学运算(加、减、乘、除)。我看了教授的视频,他的节目很好。减法命令似乎在结果前面加了一个-。divide命令的结果有点不对劲。我做错了什么 例如: 123+2 = 125 (as it should) 123 - 2 = -121 124 / 2 = 0.0161290322580645 12.4 * 10 = 124 (correct) 这是我的密码: public partial class Form1 : Form {
123+2 = 125 (as it should)
123 - 2 = -121
124 / 2 = 0.0161290322580645
12.4 * 10 = 124 (correct)
这是我的密码:
public partial class Form1 : Form
{
string operand = "", operation = "", memory = "";
public Form1()
{
InitializeComponent();
}
private void btnNum1_Click(object sender, EventArgs e)
{
txtOutput.Text += "1";
}
private void btnNum2_Click(object sender, EventArgs e)
{
txtOutput.Text += "2";
}
private void btnNum3_Click(object sender, EventArgs e)
{
txtOutput.Text += "3";
}
private void btnNum4_Click(object sender, EventArgs e)
{
txtOutput.Text += "4";
}
private void btnNum5_Click(object sender, EventArgs e)
{
txtOutput.Text += "5";
}
private void btnNum6_Click(object sender, EventArgs e)
{
txtOutput.Text += "6";
}
private void btnNum7_Click(object sender, EventArgs e)
{
txtOutput.Text += "7";
}
private void btnNum8_Click(object sender, EventArgs e)
{
txtOutput.Text += "8";
}
private void btnNum9_Click(object sender, EventArgs e)
{
txtOutput.Text += "9";
}
private void btnNum0_Click(object sender, EventArgs e)
{
txtOutput.Text += "0";
}
private void btnDec_Click(object sender, EventArgs e)
{
if (txtOutput.Text.IndexOf(".") == -1)
{
txtOutput.Text += ".";
}
}
private void btnNegPos_Click(object sender, EventArgs e)
{
if (txtOutput.Text.IndexOf("-") == -1)
{
txtOutput.Text = "-" + txtOutput.Text;
}
else
{
txtOutput.Text = txtOutput.Text.Substring(1, (txtOutput.Text.Length - 1));
}
}
private void execute()
{
if (operation == "+")
{
operand = Convert.ToString(Convert.ToDouble(txtOutput.Text) + Convert.ToDouble(operand));
}
if (operation == "-")
{
operand = Convert.ToString(Convert.ToDouble(txtOutput.Text) - Convert.ToDouble(operand));
}
if (operation == "*")
{
operand = Convert.ToString(Convert.ToDouble(txtOutput.Text) * Convert.ToDouble(operand));
}
if (operation == "/")
{
operand = Convert.ToString(Convert.ToDouble(txtOutput.Text) / Convert.ToDouble(operand));
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (operand == "")
{
operand = txtOutput.Text;
}
else
{
execute();
}
operation = "+";
txtOutput.Text = "";
}
private void btnSubtract_Click(object sender, EventArgs e)
{
if (operand == "")
{
operand = txtOutput.Text;
}
else
{
execute();
}
operation = "-";
txtOutput.Text = "";
}
private void btnMultiply_Click(object sender, EventArgs e)
{
if (operand == "")
{
operand = txtOutput.Text;
}
else
{
execute();
}
operation = "*";
txtOutput.Text = "";
}
private void btnDivide_Click(object sender, EventArgs e)
{
if (operand == "")
{
operand = txtOutput.Text;
}
else
{
execute();
}
operation = "/";
txtOutput.Text = "";
}
private void btnClear_Click(object sender, EventArgs e)
{
txtOutput.Text = "";
operand = "";
operation = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEquals_Click(object sender, EventArgs e)
{
if (operation == "+")
{
txtOutput.Text = Convert.ToString(Convert.ToDouble(txtOutput.Text) + Convert.ToDouble(operand));
}
if (operation == "-")
{
txtOutput.Text = Convert.ToString(Convert.ToDouble(txtOutput.Text) - Convert.ToDouble(operand));
}
if (operation == "*")
{
txtOutput.Text = Convert.ToString(Convert.ToDouble(txtOutput.Text) * Convert.ToDouble(operand));
}
if (operation == "/")
{
txtOutput.Text = Convert.ToString(Convert.ToDouble(txtOutput.Text) / Convert.ToDouble(operand));
}
operand = "";
operation = "";
}
private void btnBackspace_Click(object sender, EventArgs e)
{
txtOutput.Text = "";
}
}
谢谢您的时间。您已经得到了 2/124=0.0161290322580645
反转您的股息和除数;) 您正在反向操作您的参数。在等分函数中,第二个算符除以第一个算符,得到分数。在减法中,你做的是2-123,得到的是一个负数,例如,改为
txtOutput.Text = Convert.ToString( Convert.ToDouble(operand) / Convert.ToDouble(txtOutput.Text));
我并没有真正为代码操心,但这里有一个提示:2/124=0.0161290322580645我认为学习调试比简单地在StackOverflow上发布代码并询问它有什么问题要好得多。如果设置了断点,应该清楚为什么操作没有产生预期的结果。这将导致编译器错误,不能将“double”除以“string”。需要将
Convert.ToString
移到计算之外。@RonBeyer您是对的,先生,我复制/粘贴得太匆忙了。我已经修好了。毫无疑问,减法也有同样的问题。