C# bool 语句为看似未连接的 double.parse(string) 抛出奇怪的异常
C# bool statement throws strange exception for seemingly unconnected double.parse(string)
就 C# 而言,我绝对是初学者。尝试通过示例学习。所以我为自己找到了一个不错的计算器小教程。直到最后一刻一切都很好,代码可以正常工作,但它不需要像 33
这样的多位数输入。那里有一个 bool 语句用于转换算术运算 on/off 并且教程讲师认为我们应该在数字 input/button press 之前放置 bool = false
(在 button_Click
中)。
他的代码是这样的:
public partial class MainWindow : Window
{
double value = 0;
string operation = "";
bool operation_pressed = false;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if ((tb.Text == "0") || (operation_pressed == true))
tb.Clear();
operation_pressed = false;
Button b = (Button)sender;
tb.Text += "\n" + b.Content.ToString();
}
private void operator_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
operation = b.Content.ToString();
value = double.Parse(tb.Text);
operation_pressed = true;
equation.Content = value + " " + operation;
}
private void result_Click(object sender, RoutedEventArgs e)
{
equation.Content = "";
switch(operation)
{
case "+":
tb.Text = "\n" + (value + double.Parse(tb.Text)).ToString();
break;
case "-":
tb.Text = "\n" + (value - double.Parse(tb.Text)).ToString();
break;
case "*":
tb.Text = "\n" + (value * double.Parse(tb.Text)).ToString();
break;
case "/":
tb.Text = "\n" + (value / double.Parse(tb.Text)).ToString();
break;
default:
break;
}
}
private void CE_Click(object sender, RoutedEventArgs e)
{
tb.Text = "\n 0";
}
private void C_Click(object sender, RoutedEventArgs e)
{
tb.Clear();
equation.Content = "";
value = 0;
}
}
它编译得很好。但是当我尝试输入一个多位数字并在其后跟一个数学运算符时,它会抛出 value = double.Parse(tb.Text);
的异常,指出:
When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime
object.
我现在很困惑。甚至没有 DateTime
涉及!而且我 100% 肯定,一切都像教程中那样。这是怎么回事? :/
任何帮助将不胜感激!
编辑
实际错误截图:
在 operator_Click 事件中尝试此代码
Button b = (Button)sender;
operation = b.Text;
value = Convert.ToDouble(tb.text)
首先,您对调试器的解释不正确。这是不是错误信息:
When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object.
请注意它是如何列为 "Troubleshooting Tips" 的。在绝大多数情况下,您可以忽略它。错误信息本身是用我不懂的语言写的,所以我无法理解它所说的内容。但是 FormatException
本质上意味着您正在尝试解析无法解析的值。
你的截图截掉了一些信息,但是tb.Text
的值是多少?它是那些 "+"
字符串之一吗?如果是,那就是你的问题了。
"+"
无法解析为数值,因为“+”不是数字。
您可以使用 TryParse
而不是 Parse
使您的代码更能抵抗错误。像这样:
double result;
if (!double.TryParse(tb.Text, out result))
{
// couldn't parse
}
如果未输入 if
块,则 result
将包含成功解析的值。如果输入是,则无法解析该值。你如何处理这种情况取决于你。给用户的错误消息,默认值而不是解析值等。这是您定义的应用程序逻辑。
重点是,tb.Text
包含一个非数字值,您正试图将其转换为数字值。因此错误。
就 C# 而言,我绝对是初学者。尝试通过示例学习。所以我为自己找到了一个不错的计算器小教程。直到最后一刻一切都很好,代码可以正常工作,但它不需要像 33
这样的多位数输入。那里有一个 bool 语句用于转换算术运算 on/off 并且教程讲师认为我们应该在数字 input/button press 之前放置 bool = false
(在 button_Click
中)。
他的代码是这样的:
public partial class MainWindow : Window
{
double value = 0;
string operation = "";
bool operation_pressed = false;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
if ((tb.Text == "0") || (operation_pressed == true))
tb.Clear();
operation_pressed = false;
Button b = (Button)sender;
tb.Text += "\n" + b.Content.ToString();
}
private void operator_Click(object sender, RoutedEventArgs e)
{
Button b = (Button)sender;
operation = b.Content.ToString();
value = double.Parse(tb.Text);
operation_pressed = true;
equation.Content = value + " " + operation;
}
private void result_Click(object sender, RoutedEventArgs e)
{
equation.Content = "";
switch(operation)
{
case "+":
tb.Text = "\n" + (value + double.Parse(tb.Text)).ToString();
break;
case "-":
tb.Text = "\n" + (value - double.Parse(tb.Text)).ToString();
break;
case "*":
tb.Text = "\n" + (value * double.Parse(tb.Text)).ToString();
break;
case "/":
tb.Text = "\n" + (value / double.Parse(tb.Text)).ToString();
break;
default:
break;
}
}
private void CE_Click(object sender, RoutedEventArgs e)
{
tb.Text = "\n 0";
}
private void C_Click(object sender, RoutedEventArgs e)
{
tb.Clear();
equation.Content = "";
value = 0;
}
}
它编译得很好。但是当我尝试输入一个多位数字并在其后跟一个数学运算符时,它会抛出 value = double.Parse(tb.Text);
的异常,指出:
When converting string to DateTime, parse the string to take the date before putting each variable into the
DateTime
object.
我现在很困惑。甚至没有 DateTime
涉及!而且我 100% 肯定,一切都像教程中那样。这是怎么回事? :/
任何帮助将不胜感激!
编辑
实际错误截图:
在 operator_Click 事件中尝试此代码
Button b = (Button)sender;
operation = b.Text;
value = Convert.ToDouble(tb.text)
首先,您对调试器的解释不正确。这是不是错误信息:
When converting string to DateTime, parse the string to take the date before putting each variable into the DateTime object.
请注意它是如何列为 "Troubleshooting Tips" 的。在绝大多数情况下,您可以忽略它。错误信息本身是用我不懂的语言写的,所以我无法理解它所说的内容。但是 FormatException
本质上意味着您正在尝试解析无法解析的值。
你的截图截掉了一些信息,但是tb.Text
的值是多少?它是那些 "+"
字符串之一吗?如果是,那就是你的问题了。
"+"
无法解析为数值,因为“+”不是数字。
您可以使用 TryParse
而不是 Parse
使您的代码更能抵抗错误。像这样:
double result;
if (!double.TryParse(tb.Text, out result))
{
// couldn't parse
}
如果未输入 if
块,则 result
将包含成功解析的值。如果输入是,则无法解析该值。你如何处理这种情况取决于你。给用户的错误消息,默认值而不是解析值等。这是您定义的应用程序逻辑。
重点是,tb.Text
包含一个非数字值,您正试图将其转换为数字值。因此错误。