它可以用整数进行加、减、乘和除,但不能用十进制数进行加、减、乘和除。sum1变量有问题,就像给它们赋一个小数时,它们的格式是错误的。这是因为它们被声明为double,但我不知道如何用不同的方式来写。
double sum1 = 0;
double sum2 = 0;
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 += double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = false;
subButtonClicked = false;
multButtonClicked = false;
divButtonClicked = true;
}
private void deci_Click(object sender, RoutedEventArgs e)
{
string currentInput = numEnt.Text;
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;
}
}
private void clear_Click(object sender, RoutedEventArgs e)
{
numEnt.Clear();
calculations.Clear();
result.Clear();
}
}
首先将sum1
变量声明为double
。还有一件事你需要一个for loop
来检查一个字符串是否有decimal point
。相反,您可以使用string.Contains
方法。试试这段代码,希望能有所帮助
double sum1 =0.0 ;
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 /= double.Parse(numEnt.Text);
numEnt.Clear();
plusButtonClicked = false;
subButtonClicked = false;
multButtonClicked = false;
divButtonClicked = true;
}
private void deci_Click(object sender, RoutedEventArgs e)
{
string currentInput = numEnt.Text;
bool pointFound = false;
//for (int i = 0; i < currentInput.Length; i++)
//{
if(currentInput.Contains("."))//if (currentInput[i] == '.')
pointFound = true;
//}
if (pointFound == false)
numEnt.Text += ".";
}
可能点不是您的小数分隔符。在这种情况下,可以使用
double.Parse(string, IFormatProvider)
设置所需的小数分隔符。
试试这个
var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
var numberFormat = (System.Globalization.NumberFormatInfo) currentCultur .NumberFormat.Clone();
numberFormat .NumberDecimalSeparator = ".";
sum1 += double。