我试图在C#中构建一个简单的计算器应用程序,我不知道为什么在执行以下步骤时它会崩溃。
应用程序立即崩溃。我假设它与Zero()函数有关,因为这是单击Zero按钮时执行的操作。条件语句用于处理不应像连续密码那样出现的情况。这是源代码。顺便说一句,其他数字的功能是相同的。
public partial class MainWindow : Window
{
protected double firstNumber, secondNumber;
protected string textBoxContents;
protected int selectedFunction;
public MainWindow()
{
InitializeComponent();
firstNumber = 0;
secondNumber = 0;
selectedFunction = 0;
textBoxContents = "0";
}
private void Zero(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) > 0 || textBoxContents[textBoxContents.Length - 1] == '.')
{
if(selectedFunction != 0)
textBoxContents = textBoxContents + "0";
}
else if (textBoxContents == null)
{
textBoxContents = textBoxContents + "0";
}
ResultBox.Content = textBoxContents;
}
private void One(object sender, RoutedEventArgs e)
{
textBoxContents = textBoxContents + "1";
ResultBox.Content = textBoxContents;
}
private void Decimal(object sender, RoutedEventArgs e)
{
textBoxContents = textBoxContents + ".";
ResultBox.Content = textBoxContents;
}
private void Addition(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 1;
}
private void Subtraction(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 2;
}
private void Multiplication(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 3;
}
private void Division(object sender, RoutedEventArgs e)
{
firstNumber = Convert.ToDouble(textBoxContents);
textBoxContents = null;
selectedFunction = 4;
}
private void Result(object sender, RoutedEventArgs e)
{
secondNumber = Convert.ToDouble(textBoxContents);
double thirdNumber = 0;
switch (selectedFunction)
{
case 1:
thirdNumber = firstNumber + secondNumber;
break;
case 2:
thirdNumber = firstNumber - secondNumber;
break;
case 3:
thirdNumber = firstNumber * secondNumber;
break;
case 4:
thirdNumber = firstNumber / secondNumber;
break;
default:
break;
}
textBoxContents = Convert.ToString(thirdNumber);
ResultBox.Content = textBoxContents;
}
private void ClearEverything(object sender, RoutedEventArgs e)
{
textBoxContents = null;
firstNumber = 0;
secondNumber = 0;
selectedFunction = 1;
ResultBox.Content = Convert.ToString(0);
}
private void ToggleNegative(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) != 0)
{
textBoxContents = Convert.ToString(Convert.ToDouble(textBoxContents) * -1);
ResultBox.Content = textBoxContents;
}
else
ResultBox.Content = Convert.ToString(0);
}
}
答案 0 :(得分:1)
小数点分隔符已本地化,您确定使用的是正确的文化(","而不是"。")?
如果问题是这样,请查看此Stack Question
答案 1 :(得分:1)
private void Zero(object sender, RoutedEventArgs e)
{
if (Convert.ToDouble(textBoxContents) > 0 ||
textBoxContents[textBoxContents.Length - 1] == '.')
{
if(selectedFunction != 0)
textBoxContents = textBoxContents + "0";
}
else if (textBoxContents == null)
{
textBoxContents = textBoxContents + "0";
}
ResultBox.Content = textBoxContents;
}
这种逻辑看起来很糟糕。如果文本框的值为空,那么由于||
另一侧的索引器,它会爆炸。我认为这可以改写为:
private void Zero(object sender, RoutedEventArgs e)
{
var dblVal = Convert.ToDouble(textBoxContents.Text);
textBoxContents.Text = dblVal.ToString();
ResultBox.Content = textBoxContents.Text;
}
换句话说,如果文本框为空,则转化将产生0.0
;如果它以1.
结尾,则会产生1.0
;如果它.5
它会产生0.5
。只需利用Convert
。
答案 2 :(得分:1)
textBoxContents为空。
而不是textBoxContents = null;
使用textBoxContents = "0";
或textBoxContents = string.Empty;
。为什么你把它设置为null呢?
在Zero方法中调用textBoxContents.Length
会导致NullReferenceException
。
正如其他人之前提到的那样,你在Zero()中的逻辑接缝有点迂回,当然可能更小。
答案 3 :(得分:0)
你正在做的减法功能
textBoxContents = null;
然后在零中你有
textBoxContents [textBoxContents.Length - 1]
这就是崩溃的原因
你应该在textBoxContents
上的任何操作之前检查null