我正在制作一个计算器但是当我尝试使用十进制数进行计算时出现异常。所有sum1 += double.Parse(numEnt.Text);
都有问题
但我现在有点迷失了:P
public partial class MainWindow : Window
{
double sum1 = 0;
double sum2 = 0;
string dotSign = "";
bool plusButtonClicked = false;
bool subButtonClicked = false;
bool multButtonClicked = false;
bool divButtonClicked = false;
public MainWindow()
{
InitializeComponent();
}
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
private void one_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += one.Content;
}
private void two_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += two.Content;
}
private void three_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += three.Content;
}
private void four_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += four.Content;
}
private void five_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += five.Content;
}
private void six_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += six.Content;
}
private void seven_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += seven.Content;
}
private void eight_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += eight.Content;
}
private void nine_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += nine.Content;
}
private void zero_Click(object sender, RoutedEventArgs e)
{
numEnt.Text += zero.Content;
}
private void add_Click(object sender, RoutedEventArgs e)
{
sum1 += double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = true;
subButtonClicked = false;
multButtonClicked = false;
divButtonClicked = false;
}
private void sub_Click(object sender, RoutedEventArgs e)
{
sum1 += double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = false;
subButtonClicked = true;
multButtonClicked = false;
divButtonClicked = false;
}
private void mult_Click(object sender, RoutedEventArgs e)
{
sum1 += double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = false;
subButtonClicked = false;
multButtonClicked = true;
divButtonClicked = false;
}
private void div_Click(object sender, RoutedEventArgs e)
{
sum1 = Convert.ToDouble(dotSign);
sum1 += double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = false;
subButtonClicked = false;
multButtonClicked = false;
divButtonClicked = true;
}
public void deci_Click(object sender, RoutedEventArgs e)
{
double currentInput = numEnt.Text.Length;
bool pointFound = false;
/*for (int i = 0; i < currentInput.Length; i++)
{
if (currentInput[i] == '.')
pointFound = true;
}*/
if (pointFound == false)
numEnt.Text += ".";
}
private void equal_Click(object sender, RoutedEventArgs e)
{
if (plusButtonClicked == true)
{
sum2 = sum1 + double.Parse(numEnt.Text);
result.Text = sum2.ToString();
sum1 = 0;
}
if (subButtonClicked == true)
{
sum2 = sum1 - double.Parse(numEnt.Text);
result.Text = sum2.ToString();
sum1 = 0;
}
if (multButtonClicked == true)
{
sum2 = sum1 * double.Parse(numEnt.Text);
result.Text = sum2.ToString();
sum1 = 0;
}
if (divButtonClicked == true)
{
sum2 = sum1 / double.Parse(numEnt.Text);
result.Text = sum2.ToString();
sum1 = 0;
}
numEnt.Clear();
}
private void clear_Click(object sender, RoutedEventArgs e)
{
numEnt.Clear();
calculations.Clear();
result.Clear();
}
private void change_Click(object sender, RoutedEventArgs e)
{
double signchange = numEnt.Text.Length;
signchange = -signchange;
numEnt.Text = signchange.ToString();
}
}
}
答案 0 :(得分:0)
使用double.Parse(string)只有三个可能的异常,并且因为它似乎没有使用它的参数,所以你得到一个FormatException。你在谈论十进制数字,所以我猜这一切都归结为你正在使用的小数和分组。默认情况下,我认为你可以使用'。'作为小数分隔符,','或''作为组1。
您应该考虑使用double.Parse(string, NumberStyle)更准确地定义您希望在计算器中使用的分隔符。