本文介绍了C#代码开发基于Windows的计算器,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞!in my calculator all operator is functioning properly except the multiply operator. I am unable to find out the reason why it is not working? 我的尝试: 请帮我解决。What I have tried:Please help me to solve it.Double resultValue = 0; String operationPerformed = ""; bool isOperationPerformed = false; private void button_Click(object sender, EventArgs e) { if ((textBox1.Text == "0") || (isOperationPerformed)) textBox1.Clear(); isOperationPerformed = false; Button button = (Button)sender; if (button.Text == ".") { if (!textBox1.Text.Contains(".")) textBox1.Text = textBox1.Text + button.Text; } else textBox1.Text = textBox1.Text + button.Text; } private void operator_Click(object sender, EventArgs e) { Button button = (Button)sender; if (resultValue != 0) { button28.PerformClick(); operationPerformed = button.Text; resultValue = Double.Parse(textBox1.Text); label1.Text = resultValue + " " + operationPerformed; isOperationPerformed = true; } else { operationPerformed = button.Text; resultValue = Double.Parse(textBox1.Text); label1.Text = resultValue + " " + operationPerformed ; isOperationPerformed = true; } } private void buttonEqual_Click_1(object sender, EventArgs e) { switch (operationPerformed) { case "+": textBox1.Text = (resultValue + Double.Parse(textBox1.Text)).ToString(); break; case "-": textBox1.Text = (resultValue - Double.Parse(textBox1.Text)).ToString(); break; case "*": textBox1.Text = (resultValue * Double.Parse(textBox1.Text)).ToString(); break; case "/": textBox1.Text = (resultValue / Double.Parse(textBox1.Text)).ToString(); break; case "%": textBox1.Text = (resultValue % Double.Parse(textBox1.Text)).ToString(); break; default: break; } resultValue = Double.Parse(textBox1.Text); label1.Text = ""; //textBox1.ReadOnly = true; }推荐答案Input Expected output Actual output 1 2 1 2 4 4 3 6 9 4 8 16然后很明显问题出在将它加倍的位 - 它不会将自身加到自身上,或者将它乘以2,它会将它自身相乘并返回输入的平方。 所以,你可以查看代码和很明显,它在某处:Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.So with that, you can look at the code and it's obvious that it's somewhere here:private int Double(int value) { return value * value; } 一旦你知道可能出现的问题,就开始使用调试器找出原因。在方法的第一行放置一个断点,然后运行你的应用程序。当它到达断点时,调试器将停止,并将控制权移交给您。您现在可以逐行运行代码(称为单步执行)并根据需要查看(甚至更改)变量内容(哎呀,您甚至可以更改代码并在需要时再试一次)。 /> 在执行代码之前,请考虑代码中的每一行应该做什么,并将其与使用Step over按钮依次执行每一行时实际执行的操作进行比较。它符合您的期望吗?如果是这样,请转到下一行。 如果没有,为什么不呢?它有什么不同? 希望这可以帮助你找到代码的哪个部分有问题,以及问题是什么。 这是一项技能,它是一个值得开发的,因为它可以帮助你在现实世界和发展中。和所有技能一样,它只能通过使用来改进!Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.If not, why not? How does it differ?Hopefully, that should help you locate which part of that code has a problem, and what the problem is.This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!private void buttonEqual_Click_1(object sender, EventArgs e){ string number = textbox1.Text; Double value = 0.0; if (!Double.TryParse(number, out value) { // invalid text, post a message and return } switch (operationPerformed) { case "+": resultValue += value; break; case "-": resultValue -= value; break; case "*": resultValue *= value; break; case "/": resultValue /= value; break; case "%": resultValue %= value; break; default: // what button was pushed to get here? break; } textBox1.Text = resultValue.ToString(); label1.Text = "";} Quote:我无法找到它不能正常工作的原因?I am unable to find out the reason why it is not working? 当你寻求帮助时,最好解释你的代码是如何做的(工作。 您的代码没有按照您的预期行事,或者您不明白为什么! 有一个几乎通用的解决方案:运行你的代码在调试器上一步一步,检查变量。 调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。 调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向你显示正在发生的事情来帮助你。当代码不做什么时预计,你接近一个错误。 要查看你的代码在做什么:只需设置一个断点并查看你的代码执行,调试器允许你逐行执行1行并检查变量当它执行时。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 在Visual Studio中调试C#代码 - YouTube [ ^ ] 这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。When you ask for help, it is a good idea to explain how your code don(y work.Your code do not behave the way you expect, or you don't understand why !There is an almost universal solution: Run your code on debugger step by step, inspect variables.The debugger is here to show you what your code is doing and your task is to compare with what it should do.There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.Debugger - Wikipedia, the free encyclopedia[^]Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]Basic Debugging with Visual Studio 2010 - YouTube[^]Debugging C# Code in Visual Studio - YouTube[^]The debugger is here to only show you what your code is doing and your task is to compare with what it should do. 这篇关于C#代码开发基于Windows的计算器,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!