我想做最简单的计算器。 [英] I am trying to do the simplest calculator.

查看:58
本文介绍了我想做最简单的计算器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的两个盒子中获取输入并将其显示在我的标签上,当达到效果时会弹出。



这里是代码我单击+按钮时使用

I want to get the input from my two boxes and display it on my label,which pops up when a result is achieved.

here is the code I use when a "+" button is clicked

double numberOne = double.Parse(number1.Text);
double numberTwo = double.Parse(number2.Text);
result = numberOne+numberTwo;
answerLabel.Text =  result.ToString();





这是否有用,我可以在其他运营商上使用



我尝试了什么:



我尝试在每次操作时使用true和false并使用如果声明,但我只是想尝试更好更有效的东西



注意:我知道已经有一个解决方案和很多它们,我只是在尝试努力解决我的解决问题的能力,这就是我现在想出来的。



Is this going to ever work and can I use it on the other operators

What I have tried:

I tried using true and false on each operation and doing it with an if statement but I just want to try something better and more efficient

Note: I know there is already a solutions and a lot of them as well, I am just trying to work on my problem solving skills and that is what I came up with for now.

推荐答案

是的,它会起作用。

但是......我建议用而不是double.Parse你使用TryParse:

Yes, it'll work.
But...I'd suggest that instead of double.Parse you use TryParse:
double numberOne;
if (!double.TryParse(number1.Text, out numberOne))
   {
   MessageBox.Show(number1.Text + " is not a number!", "Error", MessageBoxButtons.OK);
   number1.Focus();
   return;
   }
double numberTwo;
if (!double.TryParse(number2.Text, out numberTwo))
   {
   MessageBox.Show(number2.Text + " is not a number!", "Error", MessageBoxButtons.OK);
   number2.Focus();
   return;
   }
double result = numberOne + numberTwo;
answerLabel.Text =  result.ToString();

这样,当用户犯错时,你的程序不会崩溃。

That way, when the user makes a mistake, your program doesn't just crash.


这篇关于我想做最简单的计算器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆